简体   繁体   English

使用时间频率的熊猫群

[英]Pandas Groupby using time frequency

My question is regarding a groupby of pandas dataframe. 我的问题是关于大熊猫数据框的分组依据。 A sample dataset would look like this: 样本数据集如下所示:

cust_id | date       | category
A0001   | 20/02/2016 | cat1
A0001   | 24/02/2016 | cat2
A0001   | 02/03/2016 | cat3
A0002   | 03/04/2015 | cat2

Now I want to groupby cust_id and then find events that occur within 30days of each other and compile the list of categories for those. 现在,我要对cust_id进行分组,然后查找彼此之间30天内发生的事件,并为这些事件编译类别列表。 What I have figured so far is to use pd.grouper in the following manner. 到目前为止,我已经想到的是按以下方式使用pd.grouper。

df.groupby(['cust_id', pd.Grouper(key='date', freq='30D')])['category'].apply(list)

But this isn't putting [cat1, cat2, cat3] in the same list for A0001. 但这并没有将[cat1,cat2,cat3]放在A0001的同一列表中。 Any help on what I'm doing wrong or how I can go about doing what I need would be most appreciated. 对于我在做错事情或如何去做自己需要做的事情的任何帮助,将深表感谢。

The results I want should look something like this: 我想要的结果应如下所示:

A0001 | [cat1, cat2, cat3]
A0002 | [cat2]

Thanks in Advance 提前致谢

Edit: 编辑:

Following Wen's answer, I tried and it worked for this minimum example, my bad for providing a minimum example that wasn't representative. 遵循Wen的回答,我尝试并使用了该最小示例,但是我对提供一个不具有代表性的最小示例很不好。 This can be recreated with this example for both 0.20.3 and 0.23.0 versions of pandas. 可以使用此示例为0.20.3和0.23.0版本的熊猫重新创建。

cust_id date    category
0   A0001   2015-02-02  cat5
1   A0002   2015-02-03  cat1
2   A0001   2016-02-20  cat1
3   A0001   2016-02-24  cat2
4   A0001   2016-03-02  cat3
5   A0003   2016-09-09  cat2
6   A0003   2016-08-21  cat5

The answer I get is: 我得到的答案是:

cust_id
A0001          [cat5]
A0001    [cat1, cat2]
A0001          [cat3]
A0002          [cat1]
A0003          [cat5]
Name: category, dtype: object

My apologies for the initial confusion! 对于最初的困惑,我深表歉意!

You code is work for me 您的代码对我有用

df.date=pd.to_datetime(df.date)
df.groupby(['cust_id', pd.Grouper(key='date', freq='30D')])['category'].apply(list).reset_index(level=1,drop=True)
Out[215]: 
cust_id
A0001       [ cat1,  cat2,  cat3]
A0002                     [ cat2]
Name: category, dtype: object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM