DataFrame对象没有'name'属性。

11

我目前有一组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) 时,它会打印出文件名。有人知道我做错了什么吗?

谢谢。

3个回答

5

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

创建df的副本会丢失名称:

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

'setting the value'指将值设置为某个特定的值,此时原始对象及其名称均保持不变。

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

4

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

例如:

df = pd.DataFrame()

df.columns.name = 'name'

print(df.columns.name)

name

2
我猜是因为反转导致自定义的.name属性丢失了。"Original Answer"可以翻译成"最初的答案"。
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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接