Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从python中按ID列出的客户购买日期到按ID列出的以天为单位的购买间隔_Python_Date_Dataframe_Intervals - Fatal编程技术网

从python中按ID列出的客户购买日期到按ID列出的以天为单位的购买间隔

从python中按ID列出的客户购买日期到按ID列出的以天为单位的购买间隔,python,date,dataframe,intervals,Python,Date,Dataframe,Intervals,我有一个Python数据框,其中包含客户购买记录: CUST_ID CUST_PURCHASE_DATE 0001 20140204 0001 20150102 0002 20150411 0002 20160201 0002 20160302 ..... 我希望得到: CUST_ID CUST_PURCHASE_DATE CUST_PURCHASE_INTERVALS 0001 20140204

我有一个Python数据框,其中包含客户购买记录:

CUST_ID    CUST_PURCHASE_DATE
0001       20140204
0001       20150102
0002       20150411
0002       20160201
0002       20160302
.....
我希望得到:

CUST_ID    CUST_PURCHASE_DATE    CUST_PURCHASE_INTERVALS
0001       20140204              0
0001       20150102              331
0002       20150411              0
0002       20160201              302
0002       20160302              30
.....

在Python中如何使用数据帧计算来实现这一点?谢谢

将您的专栏转换为适当的日期时间,按客户ID和
.diff()对其进行分组,例如:

pd.to_datetime(df.CUST_PURCHASE_DATE, format='%Y%m%d').groupby(df.CUST_ID).diff().fillna(0)
给你:

0     0 days
1   332 days
2     0 days
3   296 days
4    30 days
Name: CUST_PURCHASE_DATE, dtype: timedelta64[ns]