Skip to content
Advertisement

Calculating the number of starts, by customer using Pandas

I have DataFrame that looks like:

JavaScript

df

enter image description here

I’m trying to calculate the beginning and end date for each, which I think is pretty straight forward (i.e., first time each customer customer and last time, as defined by amt > 0).

What I need help with is calculating the number of new acquisitions, whether it’s their first purchase or they’ve churned for a period and come back. For example, for Customer A the first is Feb-21 and the second would be Apr-21. Moreover, Customer B would be Mar-21 and then again on Jun-21. Both would have two new

The desired output would be:

enter image description here

I’m just not sure where to start on this one.

Advertisement

Answer

One way using pandas.DataFrame.groupby with shift trick:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement