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

How to insert a text value to new column if a condition satisfied at least once within group of rows in another column

I want to insert a value "Yes" to a new column if the value "Yes" is contained at least once within the row groups in "Name" column.

df2 = pd.DataFrame({ 'Name':['John', 'Tom', 'Tom', 'Tom' ,'Ole','Ole'],
                    'SomeQty':[100, 200, 300, 400, 500,600],
                     'Match':['Yes', 'No', 'Yes','No', 'No','No'],
                    'SomeValue':[100, 200, 200, 200, 500,600]
                    })

I tried using group by transform but was not sure what function to use for the extrapolation of a text value.

df2['Match1'] = (df2.assign(Match = df2['Match'].where((df2['Match'] != "No") )).groupby(['Name'])["Match"].transform(lambda x: x))

My expected table is;
enter image description here

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

>Solution :

Check if any value in Match is Yes in groupby.transform:

df2['Match1'] = df2.groupby('Name').Match.transform(lambda g: 'Yes' if g.eq('Yes').any() else 'No')

df2
   Name  SomeQty Match  SomeValue Match1
0  John      100   Yes        100    Yes
1   Tom      200    No        200    Yes
2   Tom      300   Yes        200    Yes
3   Tom      400    No        200    Yes
4   Ole      500    No        500     No
5   Ole      600    No        600     No
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