pandas: filter rows of DataFrame with operator chaining

前端 未结 14 2319
悲哀的现实
悲哀的现实 2020-11-22 16:46

Most operations in pandas can be accomplished with operator chaining (groupby, aggregate, apply, etc), but the only way I

14条回答
  •  遥遥无期
    2020-11-22 17:22

    I had the same question except that I wanted to combine the criteria into an OR condition. The format given by Wouter Overmeire combines the criteria into an AND condition such that both must be satisfied:

    In [96]: df
    Out[96]:
       A  B  C  D
    a  1  4  9  1
    b  4  5  0  2
    c  5  5  1  0
    d  1  3  9  6
    
    In [99]: df[(df.A == 1) & (df.D == 6)]
    Out[99]:
       A  B  C  D
    d  1  3  9  6
    

    But I found that, if you wrap each condition in (... == True) and join the criteria with a pipe, the criteria are combined in an OR condition, satisfied whenever either of them is true:

    df[((df.A==1) == True) | ((df.D==6) == True)]
    

提交回复
热议问题