2 回答
TA贡献1815条经验 获得超6个赞
首先为 B 的每次出现创建组,对值求和,然后将其作为新列分配给过滤后的 df。
(
pd.Series(np.where(df.Category.eq('B'), df.index, np.nan)).bfill()
.pipe(lambda x: df.groupby(x).Value.apply(lambda x: x[:-1].sum()))
.pipe(lambda x: df[df.Category=='B'].assign(Total_A_since_previous_B=x))
)
Date Category Value Total_A_since_previous_B
2 2012-02-15 B -10 30
6 2012-10-16 B -30 90
7 2012-11-19 B -50 0
TA贡献1836条经验 获得超4个赞
使用GroupBy.agg:
blocks = df.Category.shift().eq('B').cumsum()
new_df = (df.groupby(blocks)
.agg(Date= ('Date','last'),
Category = ('Category','last'),
Value = ('Value','last'),
Total_A_since_previous_B = ('Value','sum')
)
.assign(Total_A_since_previous_B = lambda x: x.Total_A_since_previous_B
.sub(x.Value))
.reset_index(drop=True))
print(new_df)
Date Category Value Total_A_since_previous_B
0 2012-02-15 B -10 30
1 2012-10-16 B -30 90
2 2012-11-19 B -50 0
添加回答
举报