Home > database >  Group by id and change column value based on condition
Group by id and change column value based on condition

Time:06-15

I'm a bit stuck on some code. I've looked through stack and found many similar questions but all are different in some way.

I have a dataframe df_jan which looks like this.

df_jan
ID            Date          days_since_last_purchase         x_1
1           01/01/2020               0                        0
1           04/01/2020               3                        0
2           04/01/2020               0                        0
1           06/02/2020               33                       1

Basically x_1 denotes whether it has been over 30 days since their last purchase.

What I want to achieve is if an ID has x_1 = 1 anywhere in its lifetime all the x_1 values for that specific ID is set to 1 like this.

df_jan
ID               Date        days_since_last_purchase     x_1
1               01/01/2020              0                  1
1               04/01/2020              3                  1
2               04/01/2020              0                  0
1               06/02/2020              33                 1

I've tried using a .groupby function along with a .loc but it says they can't work together. I also tried modifying the answers to this without much luck.

Thank you in advance for any help you guys can give!

CodePudding user response:

You can groupby and transform, eg:

df['x_1'] = df_jan.groupby('ID')['days_since_last_purchase'].transform(lambda v: int(v.gt(30).any()))
  • Related