【发布时间】:2017-11-19 21:17:32
【问题描述】:
我正在使用 Python 2.7。我有一个看起来像的 Pandas 数据框
raw_data = {'Date': ['12/1/2016', '12/4/2016','12/23/2016', '1/18/2017','1/18/2017','1/19/2017'],
'Account': ['aa1', 'aa2','aa1', 'aa1', 'aa1', 'aa2'],
'Description': ['store1', 'store2','store1', 'store2','store1','store2' ],
'Amount': [26.43, 24.99, 31.54,45.32, 2.00, 15.41],
'Category': ['G','G','G','G','G','G'],
'Initials': ['FR','DB','FR','DB','FR','FR']}
df = pd.DataFrame(raw_data, columns = ['Date','Account','Description','Amount','Category','Initials'])
我想按每个描述和月份进行汇总,以便我的数据如下所示:
日期 说明 金额
2016 年 12 月 store1 57.97
2016 年 12 月 store2 24.99
2017 年 1 月 store1 2.00
2017 年 1 月 store2 60.73
我编写了以下按月汇总的代码,但我不知道如何合并描述列。
#convert Date to datetimeindex
df['Date'] = pd.to_datetime(df['Date'])
df = df.set_index('Date')
#sum by month
df = df.groupby(pd.TimeGrouper("M")).sum()
df
任何指导将不胜感激。谢谢。
【问题讨论】:
标签: python pandas dataframe group-by pandas-groupby