Issue
I need to calculate cumulative calculations (sum,mean,median,etc) based on the values of Indicator
It should do the calculations for all the false indicator and print it adjacent to the true. then calculated value should reset and start from true until true appears again for Indicator.
Input data frame
Amount | Indicator |
---|---|
10 | False |
20 | False |
5 | True |
8 | False |
4 | False |
6 | True |
output would be
Amount | Indicator | Sum | Mean |
---|---|---|---|
10 | False | ||
20 | False | ||
5 | True | 30 | 15 |
8 | False | ||
4 | False | ||
6 | True | 17 | 5.6 |
I tried to use groupby with cumsum() using Indicator as group , but have not had any luck yet.
Solution
In one go using only one groupby:
df[["Sum", "Mean"]] = np.NaN
df.loc[df.Indicator, ["Sum", "Mean"]] = (df.groupby(df.Indicator.cumsum())
.agg({"Amount": ["sum", "mean"]})
.shift(1).iloc[1:].values)
Answered By - bitflip
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.