pandas: filter rows of DataFrame with operator chaining

前端 未结 14 2325
悲哀的现实
悲哀的现实 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条回答
  •  旧时难觅i
    2020-11-22 17:34

    If you would like to apply all of the common boolean masks as well as a general purpose mask you can chuck the following in a file and then simply assign them all as follows:

    pd.DataFrame = apply_masks()
    

    Usage:

    A = pd.DataFrame(np.random.randn(4, 4), columns=["A", "B", "C", "D"])
    A.le_mask("A", 0.7).ge_mask("B", 0.2)... (May be repeated as necessary
    

    It's a little bit hacky but it can make things a little bit cleaner if you're continuously chopping and changing datasets according to filters. There's also a general purpose filter adapted from Daniel Velkov above in the gen_mask function which you can use with lambda functions or otherwise if desired.

    File to be saved (I use masks.py):

    import pandas as pd
    
    def eq_mask(df, key, value):
        return df[df[key] == value]
    
    def ge_mask(df, key, value):
        return df[df[key] >= value]
    
    def gt_mask(df, key, value):
        return df[df[key] > value]
    
    def le_mask(df, key, value):
        return df[df[key] <= value]
    
    def lt_mask(df, key, value):
        return df[df[key] < value]
    
    def ne_mask(df, key, value):
        return df[df[key] != value]
    
    def gen_mask(df, f):
        return df[f(df)]
    
    def apply_masks():
    
        pd.DataFrame.eq_mask = eq_mask
        pd.DataFrame.ge_mask = ge_mask
        pd.DataFrame.gt_mask = gt_mask
        pd.DataFrame.le_mask = le_mask
        pd.DataFrame.lt_mask = lt_mask
        pd.DataFrame.ne_mask = ne_mask
        pd.DataFrame.gen_mask = gen_mask
    
        return pd.DataFrame
    
    if __name__ == '__main__':
        pass
    

提交回复
热议问题