I have a dataframe with 71 columns and 30597 rows. I want to replace all non-nan entries with 1 and the nan values with 0.
Initially I tried for-loop on each value of the dataframe which was taking too much time.
Then I used data_new=data.subtract(data) which was meant to subtract all the values of the dataframe to itself so that I can make all the non-null values 0. But an error occurred as the dataframe had multiple string entries.
Use df. replace(np. nan,'',regex=True) method to replace all NaN values to an empty string in the Pandas DataFrame column.
Replacing the NaN or the null values in a dataframe can be easily performed using a single line DataFrame. fillna() and DataFrame. replace() method.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With