我正在尝试更改csv文件中的列,以便将日期和时间列合并为一个列。当我尝试在没有for循环的情况下使用特定的csv文件时,代码可以工作,但是当我尝试将代码放入循环的一部分时,它无法保存对csv文件的任何更改:
df = pd.read_csv('C:/Users/elsam/Documents/Year 3/Final EN3300 Project/Machine Learning/Data/locations/asp/row/noaa/data_id.csv')
pd.options.mode.chained_assignment = None
for f in [df, df2, df3, df4, df5, df6, df7, df8, df9, df10, df11, df12, df13, df14, df15, df16, df17, df18, df19, df20, df21, df23]:
f['Date'] = f['Date'].astype(str)
f = f.loc[(f['Date'] != '0')]
f['year'] = f['Date'].str[:-4]
f['month'] = f['Date'].str[-4:-2]
f['day'] = f['Date'].str[-2:]
f['time'] = f['time'].astype(str)
f.loc[(f['time'] == '0'), 'time'] = '000'
f.loc[(f['time'] == '2'), 'time'] = '002'
f.loc[(f['time'] == '11'), 'time'] = '011'
f.loc[(f['time'] == '20'), 'time'] = '020'
f.loc[(f['time'] == '30'), 'time'] = '030'
f.loc[(f['time'] == '40'), 'time'] = '040'
f.loc[(f['time'] == '45'), 'time'] = '045'
f.loc[(f['time'] == '46'), 'time'] = '046'
f.loc[(f['time'] == '47'), 'time'] = '047'
f.loc[(f['time'] == '53'), 'time'] = '053'
f.loc[(f['time'] == '55'), 'time'] = '055'
f.loc[(f['time'] == '56'), 'time'] = '056'
f.loc[(f['time'] == '59'), 'time'] = '059'
f['hour'] = f['time'].str[:-2]
f['minute'] = f['time'].str[-2:]
f['datetime'] = pd.to_datetime(f[['year', 'month', 'day', 'hour', 'minute']])
df.to_csv('C:/Users/elsam/Documents/Year 3/Final EN3300 Project/Machine Learning/Data/locations/asp/row/noaa/5 min NOAA.csv')
在for循环之前,我有一个要读取的csv文件列表,其格式与df csv文件的代码相同;在for循环之后,我有一个要保存的csv文件列表,其格式与df csv文件的代码相同,如上所示。但是,for循环中的任何更改似乎都没有保存到任何csv文件中。我想知道为什么这是因为当f被df、df2等替换时,代码在没有for循环的情况下工作
将您的代码更改为-
相关问题 更多 >
编程相关推荐