[英]Iterate over list (2 dataframes in 1 list)
I am importing 2 data frames at the same time:我同时导入 2 个数据框:
import pandas as pd
import numpy as np
import time
import glob
import os
msci_folder = 'C:/Users/Mike/Desktop/docs'
mscifile = glob.glob(msci_folder + "/*.csv")
dfs = []
for file in mscifile:
df = pd.read_csv(file)
dfs.append(df)
Now I want to apply codes which I was using for every individual data frame, but I get error:现在我想应用我用于每个单独数据帧的代码,但出现错误:
AttributeError: 'list' object has no attribute 'loc' AttributeError: 'list' 对象没有属性 'loc'
I try:我尝试:
for i, df in enumerate(dfs):
dfs = dfs.loc[dfs['URI'] == '/ID']
dfs.TIMESTAMP = dfs.TIMESTAMP.apply(lambda x: '%.3f' % x)
dfs.insert(0, 'Date', 0)
dfs['Date'] = [x[:8] for x in dfs['TIMESTAMP']]
dfs.to_csv('C:/Users/Mike/Desktop/docs/test.csv', index=False)
In your second for
loop:在你的第二个for
循环中:
for i, df in enumerate(dfs):
dfs = dfs.loc[dfs['URI'] == '/ID']
dfs.TIMESTAMP = dfs.TIMESTAMP.apply(lambda x: '%.3f' % x)
dfs.insert(0, 'Date', 0)
dfs['Date'] = [x[:8] for x in dfs['TIMESTAMP']]
dfs.to_csv('C:/Users/Mike/Desktop/docs/test.csv', index=False)
you used dfs
, which is the initial list you created, hence the error.您使用了dfs
,这是您创建的初始列表,因此出现错误。 You should change every instance of dfs
inside that for
loop with df
.您应该使用df
更改for
循环中的每个dfs
实例。
for i, df in enumerate(dfs):
# dfs = dfs.loc[dfs['URI'] == '/ID']
df = df.loc[df['URI'] == '/ID']
# ... and so on
# save to different files
df.to_csv(f'C:/Users/Mike/Desktop/docs/test_{i}.csv', index=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.