Customer life span is an important metric businesses with recurring customers track to make projections and is used in calculating the life time value (LTV) of a customer.
Suppose we have a table with four columns, ['date', 'customer_id', 'order_num', 'order_amount_dollars']
.
date | customer_id | order_num | order_amount_dollars | |
---|---|---|---|---|
1 | 2019-08-01 | 1 | 1 | 30 |
2 | 2019-08-05 | 2 | 2 | 40 |
3 | 2019-09-01 | 1 | 3 | 30 |
We are interested in calculating the average duration a person remains a customer.
Write a function avg_life_span(df)
which takes a dataframe and returns the average life span of the customers in the dataframe.
df = pd.DataFrame({'date': pd.to_datetime(['2019-08-01', '2019-08-05', '2019-09-01']), 'customer_id': [1, 2, 1], 'order_num': [1, 2, 3], 'order_amount_dollars': [30, 40, 30]})
date | customer_id | order_num | order_amount_dollars | |
---|---|---|---|---|
0 | 2019-08-01 00:00:00 | 1 | 1 | 30 |
1 | 2019-08-05 00:00:00 | 2 | 2 | 40 |
2 | 2019-09-01 00:00:00 | 1 | 3 | 30 |
Timedelta('15 days 12:00:00')