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 <=
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()
)