To add the remaining rows by collecting the duplicate values of a Panda specific duplicate row

Asked 2 years ago, Updated 2 years ago, 107 views

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

python pandas duplicate

2022-09-21 10:22

1 Answers

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


2022-09-21 10:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.