James Cook
James Cook

Reputation: 344

Pandas dataframe compare values == none / nothing / null

Suppose I have 2 columns in a pandas dataframe.

I want to check each row in Column A for any value that is NOT == NaN.

If a value is found then append the corresponding row with 'P' IF NaN value then 'B'

In excel I could use =if(A1="","B","P" assuming that the cell is empty and not NAN

I think my excel background is confusing me as to what is considered empty or null vs what is a NaN value.

<<Test Frame>>
Column A | Column B
1 NaN      NaN  
2 John     NaN
3 Dave     NaN
4 NaN      NaN
5 Michael  NaN

<<Desired Output>>
Column A | Column B
1 NaN      B 
2 John     P
3 Dave     P
4 NaN      B
5 Michael  P

I have done research on SO but couldn't find a fit for this particular purpose.

Upvotes: 0

Views: 1225

Answers (1)

BENY
BENY

Reputation: 323306

In pandas we have np.where

import numpy as np 

df['colB'] = np.where(df['colA'].isna(), 'B', 'P')

Upvotes: 2

Related Questions