I have a pandas datafame like this:
customer_id transaction_date ......................
0 12341 2011-05-06
1 12341 2011-06-22
2 12341 2011-05-09
3 12342 2011-06-16
4 12342 2011-07-26
5 12342 2011-03-06
6 12343 2011-05-16
7 12343 2011-04-25
8 12342 2011-07-12
9 12343 2011-05-09
10 12343 2011-04-12
11 12342 2011-07-03
I am trying to calculate the average interval a customer make transactions. In other words, every what number of days, does each customer make a transaction.
Here's what I have tried:
dummy_data.groupby(by=['customer_id'])['transaction_date'].transform('mean')
I get:
DataError: No numeric types to aggregate
How do I do this?
EDIT 1:
Expected output:
customer_id avg_days
12341 24
12342 ..
12343 ..
Explanation:
for customer with id "12341", date of first transaction is may-6, next may-9, next june 22. So, difference of days between first two transactions is 3 days, and between next transaction, 45 days. So, mean for 3 and 45 is (45+3/2 = 48/2 = 24). I would then need to do this for all customers in the dataframe.