pandas: filter rows of DataFrame with operator chaining

前端 未结 14 2367
悲哀的现实
悲哀的现实 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:25

    The answer from @lodagro is great. I would extend it by generalizing the mask function as:

    def mask(df, f):
      return df[f(df)]
    

    Then you can do stuff like:

    df.mask(lambda x: x[0] < 0).mask(lambda x: x[1] > 0)
    

提交回复
热议问题