Table of Contents
The process of replacing infinite values with the maximum value in a Pandas dataframe involves identifying and locating any infinite values within the dataframe, and then replacing them with the maximum value present in the dataframe. This can be achieved by using the appropriate functions and methods available in Pandas, such as the “replace” function and the “max” function. By implementing this method, any infinite values within the dataframe can be effectively replaced with a more appropriate and meaningful value.
Pandas: 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.0Notice 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.
The following tutorials explain how to perform other common tasks in pandas:
Cite this article
stats writer (2024). How can I replace infinite values with the maximum value in a Pandas dataframe?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-replace-infinite-values-with-the-maximum-value-in-a-pandas-dataframe/
stats writer. "How can I replace infinite values with the maximum value in a Pandas dataframe?." PSYCHOLOGICAL SCALES, 27 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-replace-infinite-values-with-the-maximum-value-in-a-pandas-dataframe/.
stats writer. "How can I replace infinite values with the maximum value in a Pandas dataframe?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-replace-infinite-values-with-the-maximum-value-in-a-pandas-dataframe/.
stats writer (2024) 'How can I replace infinite values with the maximum value in a Pandas dataframe?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-replace-infinite-values-with-the-maximum-value-in-a-pandas-dataframe/.
[1] stats writer, "How can I replace infinite values with the maximum value in a Pandas dataframe?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I replace infinite values with the maximum value in a Pandas dataframe?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
