1

check if values of two columns are same and then fill with nan in one of those columns

Suppose my df looks like:

Col A          Col B
red             red
blue            pink
green           green
orange          green
black           black

my output should be:

Col A          Col B
red              nan
blue             pink
green            nan
orange           green
black            nan

how can i achieve this?

Submitted July 28th 2020 by Admin

Answers
0

You can use Series.mask:

df['Col B'] = df['Col B'].mask(df['Col A'] == df['Col B']) Col A Col B
0 red NaN
1 blue pink
2 green NaN
3 orange green
4 black NaN

Admin | 1 year ago



Relevant Questions