Reputation: 1679
I have a df that looks like this:
time val
0 1
1 1
2 2
3 3
4 1
5 2
How do I create new columns that hold the cumulative sum of occurance of a condition? In this case, I want to create a column for each unique value in val
that holds the cumulative sum at the given row of occurences. See below:
time val sum_1 sum_2 sum_3
0 1 1 0 0
1 1 2 0 0
2 2 2 1 0
3 3 2 1 1
4 1 3 1 1
5 2 3 2 1
Upvotes: 2
Views: 971
Reputation: 2262
df['sum_1'] = (df['val'] == 1).cumsum()
df['sum_2'] = (df['val'] == 2).cumsum()
df['sum_3'] = (df['val'] == 3).cumsum()
gives
time val sum_1 sum_2 sum_3
0 0 1 1 0 0
1 1 1 2 0 0
2 2 2 2 1 0
3 3 3 2 1 1
4 4 1 3 1 1
5 5 2 3 2 1
Upvotes: 1