DataFrame对象没有属性“名称”

时间:2019-05-20 04:20:31

标签: python pandas dataframe attributeerror

我目前有一个熊猫数据框列表。我正在尝试对每个列表元素(即列表中包含的每个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

程序在我使用df.name的第二个for循环中抛出错误。

这特别奇怪,因为当我运行print(df.name)时,它会打印出文件名。有人会知道我在做什么错吗?

谢谢。

3 个答案:

答案 0 :(得分:1)

怀疑,这是丢失自定义.name属性的反转。

In [11]: df = pd.DataFrame()

In [12]: df.name = 'empty'

In [13]: df.name
Out[13]: 'empty'

In [14]: df[::-1].name
AttributeError: 'DataFrame' object has no attribute 'name'

最好存储数据帧的字典,而不要使用.name:

df_og = {file: pd.read_csv(os.path.join(data_og_dir, 'raw', fn) for fn in raw_og}

然后您可以遍历此过程并反转需要反转的值...

for fn, df in df_og.items():
    if (check_reverse(df)):
        df = df[::-1]
        df.to_csv(os.path.join(data_og_dir, 'raw_new', fn), index=False)

答案 1 :(得分:1)

解决方案是使用loc设置值,而不是创建副本。

创建df副本会丢失名称:

df = df[::-1] # creates a copy

设置值“保留”原始对象以及名称的完整性

df.loc[:] = df[:, ::-1] # reversal maintaining the original object

示例代码可沿列轴反转值:

df = pd.DataFrame([[6,10]], columns=['a','b'])
df.name='t'
print(df.name)
print(df)
df.iloc[:] = df.iloc[:,::-1]
print(df)
print(df.name)

输出:

t
   a   b
0  6  10
    a  b
0  10  6
t

答案 2 :(得分:0)

一种解决方法是设置columns.name并在需要时使用它。

示例:

df = pd.DataFrame()

df.columns.name = 'name'

print(df.columns.name)

name