pandas: filter rows of DataFrame with operator chaining

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

    Since version 0.18.1 the .loc method accepts a callable for selection. Together with lambda functions you can create very flexible chainable filters:

    import numpy as np
    import pandas as pd
    
    df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)), columns=list('ABCD'))
    df.loc[lambda df: df.A == 80]  # equivalent to df[df.A == 80] but chainable
    
    df.sort_values('A').loc[lambda df: df.A > 80].loc[lambda df: df.B > df.A]
    

    If all you're doing is filtering, you can also omit the .loc.

提交回复
热议问题