Home > Blockchain >  Create a new column==1 when another column is NaN
Create a new column==1 when another column is NaN

Time:10-25

I try to create a new column displaying 1 if another column is a missing value.

I have the following dataframe

Col1 Col2
NaN 5.5
2.5 1.6
NaN 2.0

I want to get the following result :

Col1 Col2 new_col
NaN 5.5 1.0
2.5 1.6 0.0
NaN 2.0 1.0

I tried the following code :

df['new_col']=[1 if (pd.isnull(df['col1'])==True) else 0 for i in range(len(df))]

I got the following error :

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

CodePudding user response:

df['col3']=np.where(df['Col1'].isna(), 1, 0)
df
    Col1    Col2    col3
0   NaN     5.5     1
1   2.5     1.6     0
2   NaN     2.0     1

CodePudding user response:

If you want to match any missing value per row:

df['new_col'] = df.isna().any(axis=1).astype(int)

output:

   Col1  Col2  new_col
0   5.5   NaN        1
1   2.5   1.6        0
2   NaN   2.0        1
  • Related