Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace all non-NaN entries of a dataframe with 1 and all NaN with 0

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.

like image 572
Anirban De Avatar asked May 31 '16 10:05

Anirban De


People also ask

How do you replace all NaN values with string in pandas?

Use df. replace(np. nan,'',regex=True) method to replace all NaN values to an empty string in the Pandas DataFrame column.

Which option can be used to replace NaN values by zero in all the columns?

Replacing the NaN or the null values in a dataframe can be easily performed using a single line DataFrame. fillna() and DataFrame. replace() method.


1 Answers

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 
like image 75
fmarc Avatar answered Sep 24 '22 11:09

fmarc