Skip to content Skip to sidebar Skip to footer

How To Avoid Nan When Using Np.where Function In Python?

I have a dataframe like this, col1 col2 col3 1 apple a,b 2 car c 3 dog a,c 4 dog NaN I tried to create three new columns, a,b and c, w

Solution 1:

What I will do

s=df.col2.str.get_dummies(sep=',')
Out[29]: 
   a  b  c
0  1  1  0
1  0  0  1
2  1  0  1
3  0  0  0
df=pd.concat([df,s],axis=1)

Solution 2:

You can use fillna(False). You are using Boolean indexing so always the values ​​corresponding to NaN will be 0

df['a']= np.where(df['col2'].str.contains('a').fillna(False),1,0)
df['b']= np.where(df['col2'].str.contains('b').fillna(False),1,0)
df['c']= np.where(df['col2'].str.contains('c').fillna(False),1,0)

Output:

   col1   col2 col3  ab  c
01  apple  a,b10012    car    c  10123    dog  a,c  00034    dog  NaN  000

Post a Comment for "How To Avoid Nan When Using Np.where Function In Python?"