Reputation: 35390
Let's say hypothetically I have a dataframe like this:
customer_id,purchase_date
A,2019-10-05
A,2019-10-02
A,2019-10-01
A,2019-10-03
B,2019-10-01
B,2019-10-03
C,2019-10-04
How do I generate a new column that shows the nth order progression for each customer so that I get a dataframe that looks something like this:
customer_id,purchase_date,order_num,
A,4
A,2
A,1
A,3
B,1
B,2
C,1
Upvotes: 2
Views: 109
Reputation: 150745
You can do groupby().cumcount()
:
df['order_num'] = df.sort_values('purchase_date').groupby('customer_id').cumcount() + 1
Output:
customer_id purchase_date order_num
0 A 2019-10-05 4
1 A 2019-10-02 2
2 A 2019-10-01 1
3 A 2019-10-03 3
4 B 2019-10-01 1
5 B 2019-10-03 2
6 C 2019-10-04 1
Upvotes: 3