hkml
hkml

Reputation: 339

change values in column if duplicate and corresponding meets criteria

I have a dataframe where there are duplicates in first column. In the third column there is another label but, these have duplicated data. I want to change all the duplicated data, if not matching to 'covered' if there is a duplicate in initial column and 'covered'apparent.

Picture below: SAWAD should both be changed to covered. SC should remain not covered. SCB should all change to covered

enter image description here

Upvotes: 0

Views: 27

Answers (1)

jezrael
jezrael

Reputation: 862511

Idea is test if at least one Covered per group by compre by Series.eq and GroupBy.any :

m = df['Covered/Not Covered'].eq('Covered').groupby(df['Security']).transform('any')

Or get all Security values with Covered and test column by Series.isin for all groups:

m = df['Security'].isin(df.loc[df['Covered/Not Covered'].eq('Covered'), 'Security'])

And then set original values by mask:

df.loc[m, 'Covered/Not Covered'] = 'Covered'

Upvotes: 1

Related Questions