I have a DataFrame:df as following:
row id name age url 1 e1 tom NaN http1 2 e2 john 25 NaN 3 e3 lucy NaN
Use np.where with pd.notnull to replace the missing and valid elements with 0 and 1 respectively:
np.where
pd.notnull
0
1
In [90]: df[['age', 'url']] = np.where(pd.notnull(df[['age', 'url']]), 1, 0) df Out[90]: row id name age url 0 1 e1 tom 0 1 1 2 e2 john 1 0 2 3 e3 lucy 0 1 3 4 e4 tick 1 0