Dynamically accessing a pandas dataframe column

前端 未结 2 1831
日久生厌
日久生厌 2020-12-21 06:27

Consider this simple example

import pandas as pd

df = pd.DataFrame({\'one\' : [1,2,3],
                   \'two\' : [1,0,0]})

df 
Out[9]: 
   one  two
0            


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-21 06:46

    You pass a string as the second argument. In effect, you're trying to do something like:

    df.'two'
    

    Which is invalid syntax. If you're trying to dynamically access a column, you'll need to use the index notation, [...] because the dot/attribute accessor notation doesn't work for dynamic access.


    Dynamic access on its own is possible. For example, you can use getattr (but I don't recommend this, it's an antipattern):

    In [674]: df
    Out[674]: 
       one  two
    0    1    1
    1    2    0
    2    3    0
    
    In [675]: getattr(df, 'one')
    Out[675]: 
    0    1
    1    2
    2    3
    Name: one, dtype: int64
    

    Dynamically selecting by attribute from a groupby call can be done, something like:

    In [677]: getattr(df.groupby('one'), mycol).sum() 
    Out[677]: 
    one
    1    1
    2    0
    3    0
    Name: two, dtype: int64
    

    But don't do it. It is a horrid anti pattern, and much more unreadable than df.groupby('one')[mycol].sum().

提交回复
热议问题