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

18,343

Solution 1

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.

Solution 2

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
Share:
18,343
Andrew D
Author by

Andrew D

Updated on October 04, 2022

Comments

  • Andrew D
    Andrew D less than a minute

    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"?

    • Zero
      Zero about 5 years
      pop is a reserved method of dataframe, instead use frame['pop']
  • Bharath
    Bharath about 5 years
    why use eval when you can use dict accessor
  • BENY
    BENY about 5 years
    @Bharathshetty Just because my habit .. I using query and eval the most .