[英]Pandas aggregate by year and month and sum other column
Having the following code:具有以下代码:
import pandas as pd
data = {
'x': ['2019-07-29', '2019-07-30', '2019-07-31', '2019-08-01', '2019-08-02', '2019-08-03'],
'y': [4, 6, 4, 4, 6, 7]
}
df = pd.DataFrame(data = data, columns = ['x', 'y'])
df
This will output:这将 output:
x y
0 2019-07-29 4
1 2019-07-30 6
2 2019-07-31 4
3 2019-08-01 4
4 2019-08-02 6
5 2019-08-03 7
Is it possible to group the dates in column x by year and month and sum the amounts in x and place the result in a new dataframe?是否可以按年和月对 x 列中的日期进行分组,并将 x 中的金额相加并将结果放入新的 dataframe 中? Like so:像这样:
x y
0 2019-07 13
1 2019-08 17
Use pd.to_datetime
to convert x
to pandas datetime.使用pd.to_datetime
将x
转换为 pandas 日期时间。 Then groupby
on Series.dt.year
and Series.dt.month
:然后在groupby
和Series.dt.month
上Series.dt.year
:
In [181]: df.x = pd.to_datetime(df.x)
In [194]: df = df.groupby([df.x.dt.year, df.x.dt.month]).agg(sum).rename_axis(['year', 'month']).reset_index().rename(columns={'y':'sum'})
In [195]: df
Out[195]:
year month sum
0 2019 7 14
1 2019 8 17
Something like this might work:像这样的东西可能会起作用:
>>> df.groupby(df['x'].str[:-3])['y'].sum()
x
2019-07 14
2019-08 17
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.