【发布时间】:2021-08-07 07:22:39
【问题描述】:
希望你能帮我解决这个问题。
我在 Pandas 中添加多个 CSV 文件时遇到问题。
我有 12 个具有相同列的销售数据文件(每个月一个:Sales_January_2019、Sales_February_2019.... 等等,直到 12 月)。
我尝试了以下代码,但似乎无法正常工作,而且索引号应该是连续的,并且不会在每个文件后重置。我尝试了reset_index(),但也没有用。
import pandas as pd
import glob
path = r'C:\Users\ricar\.spyder-py3\data' # my path
all_files = glob.glob(path + "/*.csv")
li = []
for filename in all_files:
df = pd.read_csv(filename, index_col=0, header=0)
li.append(df)
df.reset_index(inplace=True)
frame = pd.concat(li, axis=0, ignore_index=True)
df.drop(columns = ['x_t', 'perf'], inplace=True)
print(df)
【问题讨论】:
-
错误是什么?打印 df 时会发生什么