for instance, I have this sample dataframe
Depth Fluid
0 235.5 nan
1 236 water
2 236.5 water
3 237 nan
4 237.5 water
5 238 water
Now i want to get the sample data just to be like this
Min_Depth Max_Depth Fluid
0 236 236.5 water
1 237.5 238 water
I have read about pandas groupby
and shift
method, the logic is if previous value is still the same (water) then group it, and use min
and max
to get the Depth
, but I'm not sure if I can build the code something like what I want. I'm new in Python, I appreciate very much if someone can help me to write the code. Thanks!
CodePudding user response:
You can check with cumsum
x = df.Fluid.ne('water')
out = df[~x].groupby([x.cumsum(),df.Fluid]).agg(max_dp= ('Depth','max'),
min_dp= ('Depth','min')).reset_index(level=1)
out
Out[202]:
Fluid max_dp min_dp
Fluid
1 water 236.5 236.0
2 water 238.0 237.5