pandas: filter rows of DataFrame with operator chaining

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

    I offer this for additional examples. This is the same answer as https://stackoverflow.com/a/28159296/

    I'll add other edits to make this post more useful.

    pandas.DataFrame.query
    query was made for exactly this purpose. Consider the dataframe df

    import pandas as pd
    import numpy as np
    
    np.random.seed([3,1415])
    df = pd.DataFrame(
        np.random.randint(10, size=(10, 5)),
        columns=list('ABCDE')
    )
    
    df
    
       A  B  C  D  E
    0  0  2  7  3  8
    1  7  0  6  8  6
    2  0  2  0  4  9
    3  7  3  2  4  3
    4  3  6  7  7  4
    5  5  3  7  5  9
    6  8  7  6  4  7
    7  6  2  6  6  5
    8  2  8  7  5  8
    9  4  7  6  1  5
    

    Let's use query to filter all rows where D > B

    df.query('D > B')
    
       A  B  C  D  E
    0  0  2  7  3  8
    1  7  0  6  8  6
    2  0  2  0  4  9
    3  7  3  2  4  3
    4  3  6  7  7  4
    5  5  3  7  5  9
    7  6  2  6  6  5
    

    Which we chain

    df.query('D > B').query('C > B')
    # equivalent to
    # df.query('D > B and C > B')
    # but defeats the purpose of demonstrating chaining
    
       A  B  C  D  E
    0  0  2  7  3  8
    1  7  0  6  8  6
    4  3  6  7  7  4
    5  5  3  7  5  9
    7  6  2  6  6  5
    

提交回复
热议问题