pandas DataFrame 对象没有属性“名称”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/56214275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataFrame object has no attribute 'name'
提问by Seankala
I currently have a list of Pandas DataFrames. I'm trying to perform an operation on each list element (i.e. each DataFrame contained in the list) and then save that DataFrame to a CSV file.
我目前有一个 Pandas DataFrames 列表。我正在尝试对每个列表元素(即列表中包含的每个 DataFrame)执行操作,然后将该 DataFrame 保存到 CSV 文件。
I assigned a name
attribute to each DataFrame, but I realized that in some cases the program throws an error AttributeError: 'DataFrame' object has no attribute 'name'
.
我name
为每个 DataFrame分配了一个属性,但我意识到在某些情况下程序会抛出错误AttributeError: 'DataFrame' object has no attribute 'name'
。
Here's the code that I have.
这是我拥有的代码。
# 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
The program is throwing an error in the second for loop where I used df.name
.
该程序在我使用的第二个 for 循环中引发错误df.name
。
This is especially strange because when I run print(df.name)
it prints out the file name. Would anybody happen to know what I'm doing wrong?
这特别奇怪,因为当我运行print(df.name)
它时会打印出文件名。有人会碰巧知道我做错了什么吗?
Thank you.
谢谢你。
采纳答案by glycoaddict
the solution is to use a loc to set the values, rather than creating a copy.
解决方案是使用 loc 来设置值,而不是创建副本。
creating a copy of df loses the name:
创建 df 的副本会丢失名称:
df = df[::-1] # creates a copy
setting the value 'keeps' the original object intact, along with name
设置值“保持”原始对象完整,连同名称
df.loc[:] = df[:, ::-1] # reversal maintaining the original object
Example code that reverses values along the column axis:
沿列轴反转值的示例代码:
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)
outputs:
输出:
t
a b
0 6 10
a b
0 10 6
t
回答by Andy Hayden
I suspectit's the reversal that loses the custom .name attribute.
我怀疑这是失去自定义 .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'
You'll be better off storing a dict of dataframes rather than using .name:
最好存储数据帧的字典而不是使用 .name:
df_og = {file: pd.read_csv(os.path.join(data_og_dir, 'raw', fn) for fn in raw_og}
Then you could iterate through this and reverse the values that need reversing...
然后你可以遍历这个并反转需要反转的值......
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)
回答by salhin
A workaround is to set a columns.name
and use it when needed.
解决方法是设置 acolumns.name
并在需要时使用它。
Example:
例子:
df = pd.DataFrame()
df.columns.name = 'name'
print(df.columns.name)
name