'Find and replace "-inf" in pandas dataframe

I have a df named df_log shown below. It is a log of the original df:

             Revenue
Date    
2020-01-06  6.027700
2020-01-07  5.883267
2020-01-09  10.034421
2020-01-10  10.022058
2020-01-12  8.625719
... ...
2022-10-03  9.465970
2022-11-01  8.813104
2022-11-02  8.872754
2022-11-03  9.057518
2022-12-01  9.206497

[520 rows x 1 columns]
Revenue    float64
dtype: object

There are 3 values of '-inf' resulting from the log of 0. I want to replace '-inf' back to 0.

2020-06-08   7.931705
2020-06-09       -inf
2020-06-10  10.157778

I tried df_log = df_log.replace('-inf',0) but it didn't seem to work.

Probably something simple and easy but I need help :) thanks.



Solution 1:[1]

Those are not the actual string '-inf', just the string representation of the float value -np.inf:

df_log = df_log.replace(-np.inf, 0)

#               Revenue
# Date                 
# ...
# 2020-06-08   7.931705
# 2020-06-09   0.000000
# 2020-06-10  10.157778
# ...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1