如何在Python中按6个月对数据进行分组 [英] How to group data by 6 month in Python

查看:54
本文介绍了如何在Python中按6个月对数据进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框,我想获取每6个月的收入总和.我可以提取日期之外的季度,月份,年份,但我无法在6个月内提取出来

I have the following dataframe and I want to get the sum of the Revenue per 6 month. I can extract quarter, month, year out of the date, but I am unable to do it for the 6 month

| date      | Revenue |
|-----------|---------|
| 1/2/2017  | 200     |
| 2/2/2017  | 300     |
| 3/2/2017  | 100     |
| 4/2/2017  | 100     |
| 5/23/2017 | 200     |
| 6/20/2017 | 300     |
| 7/22/2017 | 400     |
| 8/21/2017 | 800     |
| 9/21/2017 | 500     |
| 10/21/2017| 500     |
| 11/21/2017| 500     |
| 12/21/2017| 500     |

推荐答案

您可以使用 resample .

df['date'] = pd.to_datetime(df['date'])
df.resample('6M', on='date').sum().reset_index()
#output
    date        renevue
0   2017-01-31  200
1   2017-07-31  1400
2   2018-01-31  2800

这篇关于如何在Python中按6个月对数据进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆