pandas 尝试访问数据框列显示“<bound method NDFrame.xxx...”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46239946/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:28:05  来源:igfitidea点击:

Attempt to access dataframe column displays "<bound method NDFrame.xxx..."

pythonpandasdataframe

提问by Andrew D

I create DataFrame object in Jupyter notebook:

我在 Jupyter notebook 中创建了 DataFrame 对象:

data = {'state':['Ohio','Ohio','Ohio','Nevada','Nevada'],
       'year':[2000, 2001, 2002, 2000, 2001],
       'pop':[1.5, 2.0, 3.6, 2.4, 2.9]}
frame = DataFrame(data)

When I'm extracting column 'year', it's ok:

当我提取列“年份”时,没关系:

In [30]: frame.year
Out[30]: 0    2000
         1    2001
         2    2002
         3    2000
         4    2001
         Name: year, dtype: int64

But when I'm extracting column 'pop' (frame.pop), result is:

但是当我提取列“pop”(frame.pop)时,结果是:

Out[31]:
<bound method NDFrame.pop of    pop   state  year
0  1.5    Ohio  2000
1  2.0    Ohio  2001
2  3.6    Ohio  2002
3  2.4  Nevada  2000
4  2.9  Nevada  2001>

Why the result is not the same as for "frame.year"?

为什么结果与“frame.year”不同?

采纳答案by cs95

popis a dataframe level function. The takeaway here is try to avoid using the .accessor to access columns. There are many dataframe attributes and functions that might clash with your column names, in which case the .will invoke those instead of your columns.

pop是数据帧级别的函数。这里的要点是尽量避免使用.访问器来访问列。有许多数据框属性和函数可能与您的列名发生冲突,在这种情况下,.将调用这些而不是您的列。

You want to use the [..]dict accessor instead:

您想改用[..]dict 访问器:

frame['pop']
0    1.5
1    2.0
2    3.6
3    2.4
4    2.9
Name: pop, dtype: float64


If you want to use pop, you can. This is how:

如果你想使用pop,你可以。这是如何:

frame.pop('pop') 
0    1.5
1    2.0
2    3.6
3    2.4
4    2.9
Name: pop, dtype: float64

frame
    state  year
0    Ohio  2000
1    Ohio  2001
2    Ohio  2002
3  Nevada  2000
4  Nevada  2001

Do note that this modifies the original dataframe, so don't do it unless you're trying to remove columns.

请注意,这会修改原始数据框,因此除非您尝试删除列,否则不要这样做。

回答by Daniel Severo

popis a method of pandas.DataFrame. You need to use frame['pop']

pop是一种方法pandas.DataFrame。你需要使用frame['pop']

回答by YOBEN_S

The way I am using ...eval

我使用的方式...eval

frame.eval('pop')
Out[108]: 
0    1.5
1    2.0
2    3.6
3    2.4
4    2.9
dtype: float64