Issue
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().
Solution
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
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.