Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

pandas groupby - Group By - but sum one column, and show original columns

I have a 5 column df. I need to groupby by the common names in column A, and sum column B and D. But I need to keep my output that currently sits in columns C through E. Everytime I groupby its drops columns not involved in the the grouping. I understand some columns will have 2 non common rows, for a common item in column A, and I need to display both of those values. Hope an example illustrates the problem better.

A B C D E
Apple 10 Green 1 X
Pear 15 Brown 2 Y
Pear 5 Yellow 3 Z
Banana 4 Yellow 4 P
Plum 2 Red 5 R
question from:https://stackoverflow.com/questions/65830878/group-by-but-sum-one-column-and-show-original-columns

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...