Expressing pandas subset using pipe

后端 未结 3 2234
滥情空心
滥情空心 2021-02-15 06:24

I have a dataframe that I subset like this:

   a  b   x  y
0  1  2   3 -1
1  2  4   6 -2
2  3  6   6 -3
3  4  8   3 -4

df = df[(df.a >= 2) & (df.b <=          


        
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-15 06:41

    I believe this method is clear with regard to your filtering steps and subsequent operations. Using loc[(mask1) & (mask2)] is probably more performant, however.

    >>> (df
         .pipe(lambda x: x.loc[x.a >= 2])
         .pipe(lambda x: x.loc[x.b <= 8])
         .pipe(pd.DataFrame.groupby, 'x')
         .mean()
         )
    
         a  b    y
    x             
    3  4.0  8 -4.0
    6  2.5  5 -2.5
    

    Alternatively:

    (df
     .pipe(lambda x: x.loc[x.a >= 2])
     .pipe(lambda x: x.loc[x.b <= 8])
     .groupby('x')
     .mean()
     )
    

提交回复
热议问题