pandas: filter rows of DataFrame with operator chaining

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

    My answer is similar to the others. If you do not want to create a new function you can use what pandas has defined for you already. Use the pipe method.

    df.pipe(lambda d: d[d['column'] == value])
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题