In the data below, is it possible to add the overlapping column value between A and B and its row?
For example, for A, d1 = 1+1, d2 = 3+1,
For B, d1 = 1+1, d2 = 2+3.
id, d1, d2, d3, d4
A, 1, 3, 3, 2
B, 1, 2, 2,4
A, 1, 1, 2, 4
C, 1, 2, 4, 5
D, 3, 4, 5, 5
B, 1, 3, 5, 5
This corresponds to the most basic example of pandas groupby.
>>> df = pd.DataFrame(d, columns=cols)
>>> df
id d1 d2 d3 d4
0 A 1 3 3 2
1 B 1 2 2 4
2 A 1 1 2 4
3 C 1 2 4 5
4 D 3 4 5 5
5 B 1 3 5 5
>>> df.groupby('id').sum()
d1 d2 d3 d4
id
A 2 4 5 6
B 2 5 7 9
C 1 2 4 5
D 3 4 5 5
© 2024 OneMinuteCode. All rights reserved.