Attempt to access dataframe column displays “<bound method NDFrame.xxx…”

前端 未结 3 528
你的背包
你的背包 2021-01-18 03:14

I create DataFrame object in Jupyter notebook:

data = {\'state\':[\'Ohio\',\'Ohio\',\'Ohio\',\'Nevada\',\'Nevada\'],
       \'year\':[2000, 2001, 2002, 2000,         


        
相关标签:
3条回答
  • 2021-01-18 03:31

    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.

    0 讨论(0)
  • 2021-01-18 03:35

    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
    
    0 讨论(0)
  • 2021-01-18 03:39

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

    0 讨论(0)
提交回复
热议问题