Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Group by id and change column value based on condition

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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()))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading