我在目录中有一堆文件。我想对所有文件运行一个循环,然后将结果连接到3个单独的列表,然后将它们转换为数据框。
data= pd.DataFrame({'Date':['10/2/2011', '11/2/2011', '12/2/2011', '13/2/2011'],
'Product':['Umbrella', 'Umbrella', 'Badminton', 'Shuttle'],
'Last Price':[1200, -1500, 1600, 352],
'Updated Price':[1250, 1450, 1550, 400],
'Discount':[10, 10, 10, 10]})
因此,我求和了3列,然后在循环时将结果附加到其各自的列表中。我遇到问题的地方是通过嵌套循环附加求和结果。如果有人可以在嵌套循环中出现问题的地方提供颜色,将不胜感激。
for i in files:
df= pd.read_csv(i,sep="|", low_memory = False)
Total_Uncalibrated = df[Last_Price].sum()
Total_Uncalibr = []
for Total_Uncalibrated:
uncali = Total_Uncalibrated
Total_Uncalibr.append(uncali)
Total_Calibrated = df[Discount].sum()
Total_Cali = []
for cali in Total_Cali:
df3= pd.Dataframe(Total_Calibrated)
Total_Cali.append(df3)
TotalCS01_Ab = df[Updated_Price].sum()
TotalCS01 = []
for total in TotalCS01_Abs:
df4 = pd.Dataframe(Total_Abs)
TotalCS01_Abs.append(df4)
Expected result s/b:
result_df = pd.concat([Total_Uncalibr,Total_Cali])
答案 0 :(得分:0)
您需要像这样重写所有循环:
Total_Uncalibr = []
for i in files:
df= pd.read_csv(i,sep="|", low_memory = False)
Total_Uncalibrated = df[Last_Price].sum()
Total_Uncalibr.append(Total_Uncalibrated)
您应该阅读loops in Python。