我目前有一组Pandas DataFrames列表。我试图对每个列表元素(即列表中包含的每个DataFrame)执行操作,然后将该DataFrame保存到CSV文件中。
我为每个DataFrame分配了一个name
属性,但我意识到在某些情况下程序会抛出错误AttributeError: 'DataFrame' object has no attribute 'name'
。
以下是我的代码。
# raw_og contains the file names for each CSV file.
# df_og is the list containing the DataFrame of each file.
for idx, file in enumerate(raw_og):
df_og.append(pd.read_csv(os.path.join(data_og_dir, 'raw', file)))
df_og[idx].name = file
# I'm basically checking if the DataFrame is in reverse-chronological order using the
# check_reverse function. If it is then I simply reverse the order and save the file.
for df in df_og:
if (check_reverse(df)):
df = df[::-1]
df.to_csv(os.path.join(data_og_dir, 'raw_new', df.name), index=False)
else:
continue
在第二个for循环中,我使用了 df.name
,程序抛出了一个错误。
这尤其奇怪,因为当我运行 print(df.name)
时,它会打印出文件名。有人知道我做错了什么吗?
谢谢。