Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: replace empty cell to 0

Tags:

python

pandas

I have a data frame results that contains empty cells and I would like to replace all empty cells with 0.

So far I have tried using pandas' fillna:

result.fillna(0)

and replace:

result.replace(r'\s+', np.nan, regex=True)

However, both with no success.

like image 399
ldevyataykina Avatar asked Jul 25 '16 14:07

ldevyataykina


People also ask

How do you replace blanks with 0 in Python?

apply() Method. Another method to replace blank values with NAN is by using DataFrame. apply() method and lambda functions. The apply() method allows you to apply a function along with one of the axis of the DataFrame, default 0, which is the index (row) axis.

What is a correct method to fill empty cells with a new value in pandas?

In this method, we will use “df. fillna(method='ffill')” , which is used to propagate non-null values forward or backward.


1 Answers

You are creating a copy of the dataframe but the original one is not keeping the changes, you need to specify "inplace=True" if you want the dataframe to persist the changes

result.fillna(0, inplace=True)
like image 69
elelias Avatar answered Sep 23 '22 01:09

elelias