I have a dataframe like this:
col1 col2 col3 col4
0 A A_1 8 4
1 A A_2 2 7
2 A A_3 5 5
3 A A_sum
4 B B_1 9 8
5 B B_2 6 8
6 B B_3 5 2
7 B B_sum
I want to sum the row from col3 and col4 that belong to A or B and put the sum in the index 3 and 7 and then have an ouput data frame like this:
col1 col2 col3 col4
0 A A_1 8 4
1 A A_2 2 7
2 A A_3 5 5
3 A A_sum 15 16
4 B B_1 9 8
5 B B_2 6 8
6 B B_3 5 2
7 B B_sum 20 18
Edited: Thank you @andrej it worket perfectly with the df like above, but in this one below:
col1 col2 col3 col4
0 A A_1 8 4
1 A A_2 NaN 7
2 A A_3 NaN 5
3 A A_sum NaN NaN
4 B B_1 9 8
5 B B_2 6 8
6 B B_3 5 NaN
7 B B_sum NaN NaN
when I try your solution @andrej, it gives me this output:
col1 col2 col3 col4
0 A A_1 8 4
1 A A_2 8 7
2 A A_3 8 5
3 A A_sum 8 16
4 B B_1 9 8
5 B B_2 6 8
6 B B_3 5 16
7 B B_sum 20 16
Is there a way to have the sum values only for index 3 and 7?