多个Groupby Python熊猫

时间:2018-10-18 14:17:54

标签: python-3.x pandas-groupby

我有一个以下格式的日期框架:

CUST_ID PRODUCT MONTH   DAY WEEK_DAY    HOUR
1   A   JUNE    17  0   23
1   B   JULY    20  2   15
2   B   JUNE    5   4   9
2   A   JULY    9   1   2
3   C   JUNE    22  2   10
3   C   JULY    30  3   18
3   A   JUNE    17  0   23
8   C   JULY    30  3   18
15  C   JULY    30  3   18

我想对其进行汇总,以便对每个产品和每小时的Cust_id进行计数:

PRODUCT MONTH   DAY HOUR    WEEK_DAY    COUNT_CUST_ID
A   JUNE    17  23  0   2
A   JULY    9   2   1   1
B   JUNE    5   9   4   1
B   JULY    20  15  2   1
C   JUNE    22  10  2   1
C   JULY    30  18  3   3

我尝试了这个,但是它没有给我一个“经典的”数据框:

pd.DataFrame(df.groupby(['PRODUCT','MONTH','DAY','HOUR','WEEK_DAY']).count()).groupby('CUST_ID')

0 个答案:

没有答案