Bitopan Gogoi
Bitopan Gogoi

Reputation: 125

compute a column in a dataframe by comparison of two columns

I am trying to compute a column in a dataframe and using this code-

df[col3] = df.apply(lambda x: x['col1'] == x[col2], axis=1)

It is working fine apart from cells where there is no entry. If the corresponding cells in both the columns are empty, I want it to return True but it is returning False

Upvotes: 1

Views: 29

Answers (1)

jezrael
jezrael

Reputation: 863166

I think simpliest is compare columns with repalce missing values to same scalars like nan string in Series.fillna:

df['col3'] = df['col1'].fillna('nan string') == df['col2'].fillna('nan string')

Upvotes: 1

Related Questions