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