首页 > 解决方案 > Pandas group by and sum

问题描述

Im trying to sum a column after groupby,

Here is my Data

|Day       |SMSsNumber|ShortCode|
|----------|----------|---------|
|2020-08-25|647       |26243    |
|2020-08-25|6,396     |76973    |
|2020-08-25|16,615    |51532    |
|2020-08-25|315       |59230    |
|2020-08-25|4,732     |30210    |
|2020-08-25|209       |32261    |
|2020-08-25|7         |54835    |

I already grouped by Date, but i need to sum the SMSsNumber column.

This is what I getting

|Day       |SMSsNumber|Codes|
|----------|----------|-----|
|2020-08-25|647       |26243|
|          |6,396     |76973|
|          |16,615    |51532|
|          |315       |59230|
|          |4,732     |30210|
|          |209       |32261|
|          |7         |54835|

And I need to get the info like this:

|Day       |SMSsNumber|Codes|
|----------|----------|-----|
|2020-08-25|28921     |26243|
|          |          |76973|
|          |          |51532|
|          |          |59230|
|          |          |30210|
|          |          |32261|
|          |          |54835|

This is my code

read = pd.read_csv('data.csv')
group_day = read.groupby(['Day','SMSsNumber']).sum()
group_day.to_html('test.html')
print(group_day.head())   

:c

标签: pythonpandasdataframe

解决方案


不要按 SMSsNumber 分组:

read.groupby('Day').sum()

如果您要避免使用其他列,请明确选择这些列:

read.groupby('Day')[['SMSsNumber','ShortCode']].sum()

推荐阅读