尝试访问数据框列显示“绑定方法NDFrame.xxx ...”。 [英] Attempt to access dataframe column displays "<bound method NDFrame.xxx..."

查看:55
本文介绍了尝试访问数据框列显示“绑定方法NDFrame.xxx ...”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Jupyter笔记本中创建DataFrame对象:

I create DataFrame object in Jupyter notebook:

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

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

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

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>

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

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

推荐答案

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

pop is 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.

您想使用 [..] dict访问器代替:

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

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






如果要使用 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.

这篇关于尝试访问数据框列显示“绑定方法NDFrame.xxx ...”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆