Ignore NAN value and compare the cell values and return true or false

Advertisements

I would appreciate if someone can advise on how to solve this using pandas library. as I am newbie to programing.

I want to compare each the value on each raw ignoring NAN
Note: last raw also true.

Col_1 Col_2 Col_3 Col_4 Col_5 Col_6

A01 NaN A01 NaN A02 NaN
A02 A02 A02 A02 A02 A02
A03 A03 NaN NaN NaN A03
A04 A01 NaN A04 NaN NaN
A05 A05 A05 A05 NaN NaN
A06 NaN NaN NaN NaN NaN

I want the result to be as showing on below dataframe

Col_1   Col_2   Col_3   Col_4   Col_5   Col_6   results
A01 NaN A01 NaN A02 NaN FALSE
A02 A02 A02 A02 A02 A02 TRUE
A03 A03 NaN NaN NaN A03 TRUE
A04 A01 NaN A04 NaN NaN FALSE
A05 A05 A05 A05 NaN NaN TRUE
A06 NaN NaN NaN NaN NaN TRUE

>Solution :

Use DataFrame.nunique because method omit missing values:

df['results'] = df.nunique(axis=1).eq(1)
print (df)
  Col_1 Col_2 Col_3 Col_4 Col_5 Col_6  results
0   A01   NaN   A01   NaN   A02   NaN    False
1   A02   A02   A02   A02   A02   A02     True
2   A03   A03   NaN   NaN   NaN   A03     True
3   A04   A01   NaN   A04   NaN   NaN    False
4   A05   A05   A05   A05   NaN   NaN     True
5   A06   NaN   NaN   NaN   NaN   NaN     True

Leave a ReplyCancel reply