Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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>

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

like image 593
Andrew D Avatar asked Sep 15 '17 12:09

Andrew D


2 Answers

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.

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

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

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.

like image 106
cs95 Avatar answered Sep 29 '22 21:09

cs95


The way I am using ...eval

frame.eval('pop')
Out[108]: 
0    1.5
1    2.0
2    3.6
3    2.4
4    2.9
dtype: float64
like image 24
BENY Avatar answered Sep 29 '22 23:09

BENY