Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Best Way to add group totals to a dataframe in Pandas

I have a simple task that I'm wondering if there is a better / more efficient way to do. I have a dataframe that looks like this:

  Group  Score  Count
0     A      5    100
1     A      1     50
2     A      3      5
3     B      1     40
4     B      2     20
5     B      1     60

And I want to add a column that holds the value of the group total count:

  Group  Score  Count  TotalCount
0     A      5    100         155
1     A      1     50         155
2     A      3      5         155
3     B      1     40         120
4     B      2     20         120
5     B      1     60         120

The way I did this was:

Grouped=df.groupby('Group')['Count'].sum().reset_index()
Grouped=Grouped.rename(columns={'Count':'TotalCount'})

df=pd.merge(df, Grouped, on='Group', how='left')

Is there a better / cleaner way to add these values directly to the dataframe?

Thanks for the help.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)
df['TotalCount'] = df.groupby('Group')['Count'].transform('sum')

Some other options are discussed here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...