如何一次保存多个CSV文件,并更改它们的标题?

huangapple go评论88阅读模式
英文:

How to save multiple multiple csv files at once with changing title?

问题

I'm currently opening data from over a 1,000 csv files, each file has a date attached to it. I have modified the data with some code and would like to save the same amount of files with the new data. When I run the code, the computer seems to put all the csv files in the same folder like I wanted it to, but each one that I open is the same exact data meaning it's only capturing one day's worth of data and printing it over 1,000 times.

import glob
import numpy as np
import pandas as pd  

filepaths = glob.glob('C:/Users/Freedom/Desktop/Pronimances/*.csv')

for path in filepaths:
    
    data = np.loadtxt(path, delimiter = ',', skiprows = 1, dtype = 'int', usecols = (0,1,2,3,4,5,6), unpack = True)
    times = data[0]
    nums = data[1]
    Left = data[2]
    Right = data[3]
    y = data[4]
    area = data[6]
    
    #code that manipulates data and creates new lists has been omitted

    dict = {'Time': list2, 'Prominence Number': realpromID, 'Left Edge (Pixels)': Left, 'Right Edge (Pixels)': listd, 'Prominence Area': listb}     
    
    for i in range(1323):
        
        filename = realdates[i] 
        df = pd.DataFrame(dict) 
        df.to_csv(f'E:Arman/Prominence_Tracking/{filename}.csv') 

The "realdates" list is just a list of all the dates that the files are from. Here is a picture of the results I've gotten:
Current Results

Why isn't the program working the way I want it to? Does it have something to do with the for loop. Any help would be much appreciated.

Update: Here's the error I'm getting:

FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/Freedom/Desktop/Prom_Tracking/[20090325, 20090326, 20090327, 20090330, 20090401, ... (long list of dates) ... 20140507, 20140508, 20140509, 20140511, 20140512, 20140513, 20140514, 20140515, 20140516, 20140518, ... (continued)'
英文:

I'm currently opening opening data from over a 1,000 csv files, each file has a date attached to it. I have modified the data with some code and would like to save the same amount of files with the new data. When I run the code, the computer seems to put all the csv files in the same folder like I wanted it to, but each one that I open is the same exact data meaning it's only capturing one day's worth of data and printing it over 1,000 times. Here is all the necessary code:

import glob
import numpy as np
import pandas as pd  

filepaths = glob.glob('C:/Users/Freedom/Desktop/Pronimances/*.csv')

for path in filepaths:
    
    data = np.loadtxt(path, delimiter = ',', skiprows = 1, dtype = 'int', usecols = (0,1,2,3,4,5,6), unpack = True)
    times = data[0]
    nums = data[1]
    Left = data[2]
    Right = data[3]
    y = data[4]
    area = data[6]
    
    #code that manipulates data and creates new lists has been omitted

    dict = {'Time': list2, 'Prominence Number': realpromID, 'Left Edge (Pixels)': Left, 'Right Edge (Pixels)': listd, 'Prominence Area': listb}     
    
    
    for i in range(1323):
        
        filename = realdates[i] 
        df = pd.DataFrame(dict) 
        df.to_csv(f'E:Arman/Prominence_Tracking_Test/{filename}.csv') 

The "realdates" list is just a list of all the dates that the files are from. Here is a picture of the results I've gotten:
Current Results

Why isn't the program working the way I want it to? Does it have something to do with the for loop. Any help would be much appreciated.

Update: Here's the error I'm getting:

FileNotFoundError                         Traceback (most recent call last)
Cell In[26], line 139
136 data_dict = {'Time': list2, 'Prominence Number': realpromID, 'Left Edge (Pixels)': Left, 'Right Edge (Pixels)': listd, 'Prominence Area': listb}     
138 df = pd.DataFrame(data_dict) 
--> 139 df.to_csv(f'C:/Users/Freedom/Desktop/Prom_Tracking/{realdates}.csv')
File E:\Anaconda\lib\site-packages\pandas\util\_decorators.py:211, in deprecate_kwarg.<locals>._deprecate_kwarg.<locals>.wrapper(*args, **kwargs)
209     else:
210         kwargs[new_arg_name] = new_arg_value
--> 211 return func(*args, **kwargs)
File E:\Anaconda\lib\site-packages\pandas\core\generic.py:3720, in NDFrame.to_csv(self, path_or_buf, sep, na_rep, float_format, columns, header, index, index_label, mode, encoding, compression, quoting, quotechar, lineterminator, chunksize, date_format, doublequote, escapechar, decimal, errors, storage_options)
3709 df = self if isinstance(self, ABCDataFrame) else self.to_frame()
3711 formatter = DataFrameFormatter(
3712     frame=df,
3713     header=header,
(...)
3717     decimal=decimal,
3718 )
-> 3720 return DataFrameRenderer(formatter).to_csv(
3721     path_or_buf,
3722     lineterminator=lineterminator,
3723     sep=sep,
3724     encoding=encoding,
3725     errors=errors,
3726     compression=compression,
3727     quoting=quoting,
3728     columns=columns,
3729     index_label=index_label,
3730     mode=mode,
3731     chunksize=chunksize,
3732     quotechar=quotechar,
3733     date_format=date_format,
3734     doublequote=doublequote,
3735     escapechar=escapechar,
3736     storage_options=storage_options,
3737 )
File E:\Anaconda\lib\site-packages\pandas\util\_decorators.py:211, in deprecate_kwarg.<locals>._deprecate_kwarg.<locals>.wrapper(*args, **kwargs)
209     else:
210         kwargs[new_arg_name] = new_arg_value
--> 211 return func(*args, **kwargs)
File E:\Anaconda\lib\site-packages\pandas\io\formats\format.py:1189, in DataFrameRenderer.to_csv(self, path_or_buf, encoding, sep, columns, index_label, mode, compression, quoting, quotechar, lineterminator, chunksize, date_format, doublequote, escapechar, errors, storage_options)
1168     created_buffer = False
1170 csv_formatter = CSVFormatter(
1171     path_or_buf=path_or_buf,
1172     lineterminator=lineterminator,
(...)
1187     formatter=self.fmt,
1188 )
-> 1189 csv_formatter.save()
1191 if created_buffer:
1192     assert isinstance(path_or_buf, StringIO)
File E:\Anaconda\lib\site-packages\pandas\io\formats\csvs.py:241, in CSVFormatter.save(self)
237 """
238 Create the writer & save.
239 """
240 # apply compression and byte/text conversion
--> 241 with get_handle(
242     self.filepath_or_buffer,
243     self.mode,
244     encoding=self.encoding,
245     errors=self.errors,
246     compression=self.compression,
247     storage_options=self.storage_options,
248 ) as handles:
249 
250     # Note: self.encoding is irrelevant here
251     self.writer = csvlib.writer(
252         handles.handle,
253         lineterminator=self.lineterminator,
(...)
258         quotechar=self.quotechar,
259     )
261     self._save()
File E:\Anaconda\lib\site-packages\pandas\io\common.py:856, in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)
851 elif isinstance(handle, str):
852     # Check whether the filename is to be opened in binary mode.
853     # Binary mode does not support 'encoding' and 'newline'.
854     if ioargs.encoding and "b" not in ioargs.mode:
855         # Encoding
--> 856         handle = open(
857             handle,
858             ioargs.mode,
859             encoding=ioargs.encoding,
860             errors=errors,
861             newline="",
862         )
863     else:
864         # Binary mode
865         handle = open(handle, ioargs.mode)
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/Freedom/Desktop/Prom_Tracking/[20090325, 20090326, 20090327, 20090330, 20090401, 20090402, 20090403, 20090406, 20090407, 20090408, 20090409, 20090413, 20090416, 20090417, 20090420, 20090421, 20090422, 20090504, 20090507, 20090508, 20090511, 20090512, 20090513, 20090514, 20090515, 20090520, 20090521, 20090522, 20090526, 20090527, 20090528, 20090529, 20090602, 20090608, 20090609, 20090611, 20090612, 20090615, 20090618, 20090619, 20090620, 20090622, 20090623, 20090625, 20090626, 20090629, 20090630, 20090701, 20090702, 20090706, 20090707, 20090708, 20090709, 20090710, 20090713, 20090714, 20090715, 20090716, 20090717, 20090720, 20090721, 20090722, 20090727, 20090728, 20090729, 20090730, 20090804, 20090805, 20090806, 20090807, 20090810, 20090813, 20090824, 20090825, 20090826, 20090827, 20090828, 20090831, 20090902, 20090903, 20090904, 20090908, 20090909, 20090910, 20090911, 20090914, 20090915, 20090916, 20090917, 20090918, 20090921, 20090922, 20090923, 20090924, 20090925, 20090928, 20090929, 20090930, 20091001, 20091002, 20091003, 20091005, 20091006, 20091007, 20091008, 20091009, 20091015, 20091016, 20091019, 20091020, 20091022, 20091023, 20091026, 20091028, 20091029, 20091030, 20091102, 20091103, 20091104, 20091105, 20091106, 20091110, 20091116, 20091117, 20091118, 20091119, 20091120, 20091123, 20091124, 20091125, 20091201, 20091202, 20091204, 20091209, 20091210, 20091214, 20091216, 20091217, 20091218, 20091223, 20091224, 20100105, 20100106, 20100107, 20100111, 20100113, 20100114, 20100128, 20100203, 20100211, 20100212, 20100215, 20100216, 20100218, 20100223, 20100225, 20100302, 20100303, 20100304, 20100305, 20100311, 20100312, 20100315, 20100316, 20100317, 20100318, 20100319, 20100322, 20100323, 20100324, 20100325, 20100326, 20100330, 20100331, 20100401, 20100409, 20100413, 20100414, 20100415, 20100416, 20100419, 20100420, 20100423, 20100426, 20100427, 20100428, 20100429, 20100503, 20100504, 20100505, 20100506, 20100507, 20100510, 20100511, 20100512, 20100513, 20100514, 20100518, 20100519, 20100520, 20100521, 20100524, 20100525, 20100526, 20100527, 20100601, 20100602, 20100603, 20100604, 20100607, 20100608, 20100609, 20100610, 20100611, 20100614, 20100615, 20100616, 20100617, 20100618, 20100621, 20100622, 20100623, 20100624, 20100625, 20100628, 20100629, 20100630, 20100701, 20100702, 20100707, 20100709, 20100712, 20100713, 20100714, 20100716, 20100719, 20100720, 20100721, 20100722, 20100723, 20100726, 20100727, 20100728, 20100729, 20100730, 20100802, 20100803, 20100804, 20100805, 20100806, 20100809, 20100810, 20100811, 20100812, 20100813, 20100816, 20100819, 20100820, 20100823, 20100824, 20100826, 20100827, 20100830, 20100831, 20100901, 20100902, 20100903, 20100907, 20100908, 20100909, 20100910, 20100913, 20100914, 20100915, 20100916, 20100917, 20100920, 20100921, 20100922, 20100923, 20100924, 20100927, 20101004, 20101005, 20101007, 20101008, 20101011, 20101012, 20101013, 20101015, 20101020, 20101022, 20101026, 20101027, 20101028, 20101029, 20101101, 20101103, 20101104, 20101105, 20101109, 20101110, 20101111, 20101112, 20101115, 20101116, 20101117, 20101118, 20101122, 20101123, 20101124, 20101129, 20101130, 20101201, 20101202, 20101206, 20101207, 20101208, 20101209, 20101213, 20101214, 20101223, 20110104, 20110107, 20110110, 20110112, 20110113, 20110115, 20110116, 20110118, 20110119, 20110120, 20110121, 20110124, 20110126, 20110127, 20110128, 20110131, 20110201, 20110202, 20110203, 20110204, 20110207, 20110208, 20110209, 20110210, 20110211, 20110214, 20110217, 20110222, 20110223, 20110224, 20110225, 20110228, 20110302, 20110303, 20110304, 20110308, 20110309, 20110310, 20110311, 20110314, 20110315, 20110316, 20110318, 20110323, 20110324, 20110328, 20110329, 20110330, 20110331, 20110401, 20110404, 20110405, 20110407, 20110411, 20110412, 20110413, 20110414, 20110415, 20110418, 20110419, 20110420, 20110421, 20110425, 20110426, 20110427, 20110428, 20110511, 20110512, 20110513, 20110519, 20110520, 20110523, 20110524, 20110525, 20110526, 20110527, 20110531, 20110601, 20110602, 20110603, 20110606, 20110607, 20110608, 20110609, 20110610, 20110613, 20110614, 20110615, 20110616, 20110617, 20110620, 20110621, 20110622, 20110623, 20110624, 20110628, 20110629, 20110630, 20110701, 20110707, 20110708, 20110709, 20110710, 20110711, 20110712, 20110713, 20110714, 20110715, 20110716, 20110717, 20110718, 20110719, 20110720, 20110721, 20110722, 20110723, 20110724, 20110726, 20110727, 20110728, 20110730, 20110801, 20110802, 20110803, 20110804, 20110805, 20110806, 20110807, 20110808, 20110809, 20110810, 20110812, 20110813, 20110814, 20110815, 20110816, 20110817, 20110818, 20110819, 20110820, 20110821, 20110823, 20110824, 20110825, 20110827, 20110828, 20110829, 20110830, 20110831, 20110901, 20110902, 20110903, 20110904, 20110906, 20110907, 20110908, 20110909, 20110910, 20110911, 20110912, 20110914, 20110915, 20110916, 20110917, 20110918, 20110919, 20110920, 20110921, 20110923, 20110924, 20110925, 20110926, 20110927, 20110928, 20110929, 20110930, 20111001, 20111002, 20111003, 20111004, 20111006, 20111007, 20111010, 20111011, 20111012, 20111013, 20111014, 20111015, 20111017, 20111018, 20111019, 20111020, 20111021, 20111025, 20111026, 20111027, 20111028, 20111031, 20111101, 20111103, 20111107, 20111108, 20111109, 20111114, 20111115, 20111116, 20111117, 20111118, 20111122, 20111130, 20111205, 20111206, 20111207, 20111208, 20111209, 20111214, 20111215, 20111219, 20111221, 20111222, 20111223, 20120103, 20120104, 20120105, 20120106, 20120109, 20120110, 20120112, 20120113, 20120117, 20120118, 20120119, 20120120, 20120124, 20120125, 20120126, 20120130, 20120131, 20120201, 20120202, 20120203, 20120208, 20120209, 20120210, 20120214, 20120217, 20120218, 20120219, 20120221, 20120222, 20120223, 20120224, 20120229, 20120301, 20120302, 20120306, 20120307, 20120308, 20120309, 20120313, 20120314, 20120315, 20120316, 20120320, 20120321, 20120322, 20120323, 20120327, 20120328, 20120329, 20120330, 20120402, 20120403, 20120404, 20120405, 20120409, 20120410, 20120412, 20120416, 20120417, 20120418, 20120419, 20120420, 20120423, 20120424, 20120427, 20120430, 20120501, 20120502, 20120503, 20120504, 20120507, 20120508, 20120509, 20120510, 20120511, 20120514, 20120516, 20120517, 20120518, 20120520, 20120521, 20120522, 20120523, 20120524, 20120525, 20120527, 20120529, 20120530, 20120531, 20120601, 20120602, 20120603, 20120604, 20120605, 20120606, 20120607, 20120609, 20120610, 20120611, 20120612, 20120613, 20120614, 20120615, 20120617, 20120618, 20120619, 20120620, 20120621, 20120622, 20120623, 20120624, 20120625, 20120626, 20120627, 20120628, 20120629, 20120630, 20120701, 20120702, 20120703, 20120705, 20120706, 20120707, 20120708, 20120709, 20120710, 20120711, 20120714, 20120715, 20120716, 20120717, 20120719, 20120720, 20120721, 20120723, 20120724, 20120725, 20120727, 20120728, 20120729, 20120730, 20120731, 20120801, 20120802, 20120803, 20120806, 20120807, 20120808, 20120809, 20120810, 20120812, 20120814, 20120815, 20120818, 20120819, 20120820, 20120821, 20120823, 20120824, 20120825, 20120826, 20120827, 20120828, 20120829, 20120830, 20120831, 20120901, 20120903, 20120906, 20120907, 20120908, 20120911, 20120912, 20120913, 20120914, 20120916, 20120917, 20120918, 20120919, 20120920, 20120921, 20120924, 20120925, 20120926, 20120927, 20120928, 20120929, 20120930, 20121001, 20121002, 20121003, 20121004, 20121005, 20121009, 20121010, 20121011, 20121015, 20121016, 20121017, 20121018, 20121019, 20121022, 20121023, 20121025, 20121027, 20121031, 20121101, 20121102, 20121103, 20121106, 20121107, 20121113, 20121114, 20121119, 20121120, 20121121, 20121126, 20121128, 20121203, 20121204, 20121205, 20121206, 20121207, 20121211, 20121217, 20121219, 20121220, 20121221, 20130102, 20130103, 20130104, 20130107, 20130108, 20130111, 20130114, 20130115, 20130116, 20130118, 20130122, 20130129, 20130130, 20130131, 20130204, 20130205, 20130206, 20130207, 20130212, 20130214, 20130215, 20130221, 20130222, 20130225, 20130226, 20130227, 20130228, 20130301, 20130304, 20130305, 20130306, 20130311, 20130314, 20130318, 20130319, 20130321, 20130322, 20130325, 20130326, 20130327, 20130328, 20130401, 20130402, 20130403, 20130404, 20130405, 20130409, 20130410, 20130411, 20130412, 20130415, 20130416, 20130417, 20130423, 20130424, 20130426, 20130429, 20130430, 20130501, 20130503, 20130509, 20130510, 20130513, 20130514, 20130515, 20130516, 20130517, 20130520, 20130521, 20130523, 20130524, 20130526, 20130528, 20130529, 20130530, 20130603, 20130604, 20130605, 20130606, 20130607, 20130609, 20130611, 20130612, 20130613, 20130614, 20130617, 20130618, 20130619, 20130620, 20130621, 20130625, 20130626, 20130627, 20130628, 20130703, 20130708, 20130709, 20130713, 20130715, 20130716, 20130717, 20130718, 20130722, 20130723, 20130724, 20130725, 20130727, 20130728, 20130729, 20130730, 20130731, 20130801, 20130802, 20130803, 20130804, 20130805, 20130806, 20130807, 20130808, 20130809, 20130810, 20130811, 20130812, 20130813, 20130814, 20130815, 20130816, 20130817, 20130818, 20130819, 20130820, 20130821, 20130822, 20130823, 20130824, 20130825, 20130827, 20130828, 20130830, 20130903, 20130904, 20130905, 20130906, 20130909, 20130910, 20130911, 20130912, 20130913, 20130914, 20130915, 20130916, 20130917, 20130918, 20130919, 20130920, 20130923, 20130927, 20130928, 20130929, 20130930, 20131001, 20131002, 20131004, 20131006, 20131008, 20131010, 20131015, 20131016, 20131017, 20131018, 20131019, 20131025, 20131026, 20131030, 20131031, 20131101, 20131104, 20131105, 20131106, 20131107, 20131108, 20131113, 20131114, 20131115, 20131118, 20131119, 20131120, 20131125, 20131126, 20131127, 20131203, 20131204, 20131205, 20131206, 20131209, 20131210, 20131211, 20131213, 20131216, 20131217, 20131220, 20131222, 20131223, 20131224, 20140106, 20140108, 20140110, 20140114, 20140115, 20140116, 20140118, 20140119, 20140120, 20140122, 20140123, 20140125, 20140127, 20140128, 20140129, 20140201, 20140203, 20140205, 20140208, 20140209, 20140210, 20140211, 20140212, 20140214, 20140217, 20140218, 20140219, 20140220, 20140221, 20140222, 20140223, 20140224, 20140225, 20140305, 20140306, 20140307, 20140308, 20140309, 20140310, 20140312, 20140314, 20140315, 20140316, 20140318, 20140321, 20140322, 20140324, 20140327, 20140329, 20140331, 20140406, 20140407, 20140408, 20140410, 20140413, 20140414, 20140415, 20140419, 20140420, 20140421, 20140423, 20140424, 20140425, 20140427, 20140428, 20140502, 20140503, 20140504, 20140505, 20140507, 20140508, 20140510, 20140511, 20140512, 20140515, 20140516, 20140517, 20140519, 20140521, 20140523, 20140524, 20140525, 20140526, 20140527, 20140529, 20140531, 20140601, 20140602, 20140603, 20140604, 20140606, 20140607, 20140611, 20140612, 20140614, 20140615, 20140616, 20140618, 20140619, 20140620, 20140622, 20140623, 20140624, 20140625, 20140627, 20140628, 20140630, 20140701, 20140702, 20140703, 20140704, 20140706, 20140707, 20140709, 20140710, 20140711, 20140712, 20140713, 20140715, 20140717, 20140718, 20140720, 20140721, 20140723, 20140724, 20140725, 20140726, 20140729, 20140730, 20140731, 20140801, 20140804, 20140805, 20140807, 20140809, 20140810, 20140811, 20140812, 20140813, 20140814, 20140817, 20140818, 20140820, 20140821, 20140822, 20140824, 20140826, 20140827, 20140831, 20140901, 20140902, 20140903, 20140904, 20140906, 20140909, 20140911, 20140912, 20140913, 20140914, 20140915, 20140918, 20140919, 20140920, 20140921, 20140922, 20140923, 20140924, 20140925, 20140926, 20140927, 20140928, 20140929, 20140930, 20141002, 20141004, 20141005, 20141016, 20141017, 20141018, 20141019, 20141021, 20141022, 20141023, 20141024, 20141025, 20141026, 20141028, 20141029, 20141030, 20141031, 20141103, 20141104, 20141105, 20141107, 20141110, 20141111, 20141112, 20141113, 20141118, 20141120, 20141121, 20141124, 20141125, 20141126, 20141127, 20141208, 20141209, 20141210, 20141213, 20141219, 20141222, 20141223, 20150105, 20150106, 20150107, 20150112, 20150114, 20150115, 20150121, 20150122, 20150123, 20150127, 20150202, 20150203, 20150204, 20150205, 20150206, 20150209, 20150210, 20150211, 20150212, 20150213, 20150216, 20150217, 20150218, 20150219, 20150224, 20150225, 20150226, 20150227, 20150303, 20150304, 20150305, 20150306, 20150309, 20150310, 20150312, 20150313, 20150316, 20150319, 20150320, 20150323, 20150324, 20150325, 20150326, 20150327, 20150330, 20150331, 20150401, 20150402, 20150403, 20150413, 20150414, 20150415, 20150416, 20150417, 20150418, 20150419, 20150420, 20150421, 20150426, 20150427, 20150428, 20150429, 20150430, 20150501, 20150502, 20150503, 20150504, 20150505, 20150506, 20150507, 20150509, 20150510, 20150511, 20150512, 20150513, 20150518, 20150519, 20150520, 20150521, 20150523, 20150524, 20150525, 20150527, 20150528, 20150529, 20150530, 20150531, 20150601, 20150602, 20150603, 20150605, 20150606, 20150607, 20150608, 20150610, 20150611, 20150612, 20150614, 20150615, 20150616, 20150617, 20150618, 20150619, 20150620, 20150621, 20150622, 20150623, 20150624, 20150625, 20150627, 20150630, 20150704, 20150705, 20150706, 20150707, 20150708, 20150709, 20150710, 20150711, 20150712, 20150713, 20150714, 20150715, 20150716, 20150717, 20150719, 20150721, 20150722, 20150723, 20150724, 20150725, 20150726, 20150727, 20150728, 20150731, 20150801].csv'

Update 2: Here's how I created the list realdates, it's from the original filenames themselves.

import os
# Get the list of all files and directories
path = "C:/Users/Freedom/Desktop/Pronimances/"
dir_list = os.listdir(path)
dates_str = []
for i in range (0,1323):
dates_str.append(dir_list[i][0:8])
realdates = list(map(int, dates_str))

答案1

得分: 0

根据提供的细节,似乎您正试图将realdatefilepath匹配,但代码中有嵌套循环,每次为每个输入创建1323个文件,然后每次都会覆盖它,因此最终所有1323个文件将只包含最后读取的文件的详细信息。

更新了您的代码如下:

import glob
import numpy as np
import pandas as pd  

filepaths = glob.glob('C:/Users/Freedom/Desktop/Pronimances/*.csv')

for path, real_date in zip(filepaths, realdates):	
    data = np.loadtxt(path, delimiter = ',', skiprows = 1, dtype = 'int', usecols = (0,1,2,3,4,5,6), unpack = True)
    times = data[0]
    nums = data[1]
    Left = data[2]
    Right = data[3]
    y = data[4]
    area = data[6]
    
    #省略了处理数据和创建新列表的代码
    
    data_dict = {'Time': list2, 'Prominence Number': realpromID, 'Left Edge (Pixels)': Left, 'Right Edge (Pixels)': listd, 'Prominence Area': listb}	 
         
    df = pd.DataFrame(data_dict) 
    df.to_csv(f'E:Arman/Prominence_Tracking_Test/{real_date}.csv')

根据提供的信息,这应该可以工作。

英文:

Based on the details, it seems you are trying to match realdate with filepath, but the code has nested loops, which create the 1323 files for each input, then overwrite it every time, therefore, in the end, all the 1323 files would've details from the last read file only.

Updated your code to follows:

import glob
import numpy as np
import pandas as pd  
filepaths = glob.glob('C:/Users/Freedom/Desktop/Pronimances/*.csv')
for path, real_date in zip(filepaths, realdates):	
data = np.loadtxt(path, delimiter = ',', skiprows = 1, dtype = 'int', usecols = (0,1,2,3,4,5,6), unpack = True)
times = data[0]
nums = data[1]
Left = data[2]
Right = data[3]
y = data[4]
area = data[6]
#code that manipulates data and creates new lists has been omitted
data_dict = {'Time': list2, 'Prominence Number': realpromID, 'Left Edge (Pixels)': Left, 'Right Edge (Pixels)': listd, 'Prominence Area': listb}	 
df = pd.DataFrame(data_dict) 
df.to_csv(f'E:Arman/Prominence_Tracking_Test/{real_date}.csv') 

This should work, based on the information provided.

huangapple
  • 本文由 发表于 2023年7月18日 05:40:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76708242.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定