Pandas Check multiple columns for condition

Viewed 9006

I have a dataframe and I am checking whether it is Y in all columns, else return N and also if all the columns in the rows are Null to return Null instead.

di = {'col1': [None, 'Y', 'N'], 'col2': [None, 'Y', 'N'], 'col3': [None, 'N', 'N']}
df = pd.Dataframe(di)
df['test'] = pd.np.where(df[['col1', 'col2', 'col3']].eq('Y').any(1, skipna=True), 'Y', 'N')

This return:

 col1  col2  col3 test
0  None None  None    N
1     Y     Y     N    Y
2     N     N     N    N

And I would like it to return

  col1  col2  col3 test
0  None  None  None    None
1     Y     Y     N    Y
2     N     N     N    N
2 Answers
Related