Pandas: How to Replace inf with Max Value


You can use the following methods to replace inf and -inf values with the max value in a pandas DataFrame:

Method 1: Replace inf with Max Value in One Column

#find max value of column
max_value = np.nanmax(df['my_column'][df['my_column'] != np.inf])

#replace inf and -inf in column with max value of column 
df['my_column'].replace([np.inf, -np.inf], max_value, inplace=True)

Method 2: Replace inf with Max Value in All Columns

#find max value of entire data frame
max_value = np.nanmax(df[df != np.inf])

#replace inf and -inf in all columns with max value
df.replace([np.inf, -np.inf], max_value, inplace=True)

The following examples show how to use this syntax in practice with the following pandas DataFrame:

import pandas as pd
import numpy as np

#create DataFrame
df = pd.DataFrame({'points': [18, np.inf, 19, np.inf, 14, 11, 20, 28],
                   'assists': [5, 7, 7, 9, 12, 9, 9, np.inf],
                   'rebounds': [np.inf, 8, 10, 6, 6, -np.inf, 9, 12]})

#view DataFrame
print(df)

   points  assists  rebounds
0    18.0      5.0       inf
1     inf      7.0       8.0
2    19.0      7.0      10.0
3     inf      9.0       6.0
4    14.0     12.0       6.0
5    11.0      9.0      -inf
6    20.0      9.0       9.0
7    28.0      inf      12.0

Example 1: Replace inf with Max Value in One Column

The following code shows how to replace the inf and -inf values in the rebounds column with the max value of the rebounds column:

#find max value of rebounds
max_value = np.nanmax(df['rebounds'][df['rebounds'] != np.inf])

#replace inf and -inf in rebounds with max value of rebounds
df['rebounds'].replace([np.inf, -np.inf], max_value, inplace=True)

#view updated DataFrame
print(df)

   points  assists  rebounds
0    18.0      5.0      12.0
1     inf      7.0       8.0
2    19.0      7.0      10.0
3     inf      9.0       6.0
4    14.0     12.0       6.0
5    11.0      9.0      12.0
6    20.0      9.0       9.0
7    28.0      inf      12.0

Notice that each inf and -inf value in the rebounds column has been replaced with the max value in that column of 12.

Example 2: Replace inf with Max Value in All Columns

The following code shows how to replace the inf and -inf values in every column with the max value of the entire data frame:

#find max value of entire data frame
max_value = np.nanmax(df[df != np.inf])

#replace all inf and -inf with max value
df.replace([np.inf, -np.inf], max_value, inplace=True)

#view updated DataFrame
print(df)

   points  assists  rebounds
0    18.0      5.0      28.0
1    28.0      7.0       8.0
2    19.0      7.0      10.0
3    28.0      9.0       6.0
4    14.0     12.0       6.0
5    11.0      9.0      28.0
6    20.0      9.0       9.0
7    28.0     28.0      12.0

Notice that each inf and -inf value in every column has been replaced with the max value in the entire data frame of 28.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Impute Missing Values in Pandas
How to Count Missing Values in Pandas
How to Fill NaN Values with Mean in Pandas

Leave a Reply

Your email address will not be published.