pandas 群使用时间频率 [英] Pandas Groupby using time frequency

查看:59
本文介绍了 pandas 群使用时间频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于大熊猫数据框的分组依据.样本数据集如下所示:

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

现在,我要对cust_id进行分组,然后查找彼此之间30天内发生的事件,并为这些事件编制类别列表.到目前为止,我已经想到的是按以下方式使用pd.grouper.

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. What I have figured so far is to use pd.grouper in the following manner.

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

但是,这并未将[cat1,cat2,cat3]放在A0001的同一列表中.对于我在做错事情或如何去做自己需要做的事情的任何帮助,将不胜感激.

But this isn't putting [cat1, cat2, cat3] in the same list for 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]

预先感谢

按照Wen的回答,我尝试了并将其用于此最低限度的示例,这对提供不具有代表性的最低限度的示例不利.此示例可以针对0.20.3和0.23.0版本的熊猫重新创建.

Following Wen's answer, I tried and it worked for this minimum example, my bad for providing a minimum example that wasn't representative. This can be recreated with this example for both 0.20.3 and 0.23.0 versions of pandas.

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

我得到的答案是:

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

对于最初的困惑,我深表歉意!

My apologies for the initial confusion!

推荐答案

您的代码对我有用

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

这篇关于 pandas 群使用时间频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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