Reputation: 117
I have a data frame with a list of customers ID and that date of purchase when a customer is repeated I need a new column with the number of days between the purchase date and the previous record purchase rate
Cust Data Days
123 2018-08-01
123 2018-09-01 31
124 2018-05-05
125 2017-01-25
125 2017-02-12 18
125 2017-06-14 122
Any suggestion?
Upvotes: 0
Views: 34
Reputation: 2598
You should first convert 'Data'
to datetime format.
df['Data'] = pd.to_datetime(df['Data'])
And then, get the difference between days:
df[['Cust','Data']].groupby(['Cust']).diff()
Data
0 NaT
1 31 days
2 NaT
3 NaT
4 18 days
5 122 days
To remove NaT (NaN of datetime) you can either drop them (df.dropna()
) or fill them (df.fillna(0)
).
Upvotes: 1