python - 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
解决方案
不要按 SMSsNumber 分组:
read.groupby('Day').sum()
如果您要避免使用其他列,请明确选择这些列:
read.groupby('Day')[['SMSsNumber','ShortCode']].sum()
推荐阅读
- google-cloud-platform - 通过 GCp 控制台 RDP 访问 GCP 窗口服务器
- javascript - 无法使用以下代码在 chrome 浏览器中查看 Base64 PDF?
- c - 存储到“char”类型的空指针
- python-3.x - 如何使用python从whatsapp web中提取二维码?
- angular - 如何使用 Quill 文本编辑器上传屏幕截图
- r - 如何在没有 R 中的空网站问题的情况下进行网页抓取?
- flutter - 颤振模糊导入图像
- php - 将用户插入 mysqli 数据库,但表单有错误
- android - Android Frida 拦截 HTTP 请求/响应
- c# - 如何使用 Razor C# 语法为 html 文本框元素手动创建 id 值?