df_save =df_orig.loc[:, ["A", "C", "E"]]
df_agg = df_orig.groupby("A").agg({"B": "sum", "D" : "sum"}).reset_index()
df_merged = df_save.merge(df_agg)
for c in ["B", "D"] :
df_merged.loc[df_merged[c].duplicated(), c] = ''
A |
C |
E |
B |
D |
Apple |
Green |
X |
10 |
1 |
Pear |
Brown |
Y |
155 |
23 |
Pear |
Yellow |
Z |
|
|
Banana |
Yellow |
P |
4 |
4 |
Plum |
Red |
R |
2 |
5 |
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…