You can take the return value of df.notnull()
, which is False
where the DataFrame contains NaN
and True
otherwise and cast it to integer, giving you 0
where the DataFrame is NaN
and 1
otherwise:
newdf = df.notnull().astype('int')
If you really want to write into your original DataFrame, this will work:
df.loc[~df.isnull()] = 1 # not nan
df.loc[df.isnull()] = 0 # nan
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…