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))
>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
