Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create indicator for missing values in a data frame in python

Tags:

python

pandas

I would like to create an indicator column in my data frame which shows me if values are missing in other columns. For example:

| var_1 | var_2 | indicator|  
--------------------------
|   3   |  2   |  1  |
|  NaN  |  4   |  2  |
|   1   | NaN  |  3  |

As you can see, the new column "indicator" should be 1 if no value is missing in var_1 and var_2, it should be 2 if only var_1 is missing and 3 if only var_2 is missing. Some piece of code would be very helpful. Thank you!

like image 874
Lisa Avatar asked Oct 20 '25 05:10

Lisa


1 Answers

Use np.select() which is fast too.

import numpy as np
df['indicator']=np.select([df.var_1.isnull(),df.var_2.isnull()],[2,3],1)
print(df)

   var_1  var_2  indicator
0    3.0    2.0          1
1    NaN    4.0          2
2    1.0    NaN          3
like image 197
anky Avatar answered Oct 21 '25 19:10

anky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!