If i have a DataFrame, how can I compute the sum of a columns N rows based on the value of another column being consistent(?)
Example of dataframe:
C1 C2 C3
0 400 F 31
1 10 F 32
2 300 F 33
3 100 Kn 29
4 3000 Kn 28
5 200 Kn 26
6 10 F 30
7 5000 F 34
8 30000 Kn 28
9 30000 Kn 26
Now the complicated part, I would need the sum of the C1 rows with the same C2 name until the C2 name changes. So, first the sum of three first F, then sum of following 3 Kn, then sum of 2 F etc.
Example of example output:
C1 C2 C3 sum
0 400 F 31
1 10 F 32
2 300 F 33 710
3 100 Kn 29
4 3000 Kn 28
5 200 Kn 26 3300
6 10 F 30
7 5000 F 34 5010
8 30000 Kn 28
9 30000 Kn 26 60000
I can retrieve all rows based on C2 value with df.loc[df['C2'] == 'F'], but then I can only get sum of all F rows, which is not what I want.
How can i get the sum of each F value until Kn appears, then get sum of Kn value until F appears, etc.
I'm having a hard time constructing this question to make sense, feel free to come with ideas on how I can improve the phrasing.