Pandas: Why are double brackets needed to select column after boolean indexing

前端 未结 4 1121
南笙
南笙 2021-01-29 20:08

For a df table like below,

   A B C D
0  0 1 1 1
1  2 3 5 7
3  3 1 2 8

why are the double brackets needed for selecting specific columns after

4条回答
  •  礼貌的吻别
    2021-01-29 21:07

    Because you have no columns named 'A','C', which is what you'd be trying to do which will raise a KeyError, so you have to use an iterable to sub-select from the df.

    So

    df[df['A'] < 3]['A','C']
    

    raises

    KeyError: ('A', 'C')

    Which is different to

    In [261]:
    df[df['A'] < 3][['A','C']]
    
    Out[261]:
       A  C
    0  0  1
    1  2  5
    

    This is no different to trying:

    df['A','C']
    

    hence why you need double square brackets:

    df[['A','C']]
    

    Note that the modern way is to use .ix:

    In [264]:
    df.ix[df['A'] < 3,['A','C']]
    
    Out[264]:
       A  C
    0  0  1
    1  2  5
    

    So that you're operating on a view rather than potentially a copy

提交回复
热议问题