Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to replace infinite value with maximum value of a pandas column?

I have a dataframe which looks like

City   Crime_Rate

A      10

B      20 

C      inf

D      15 

I want to replace the inf with the max value of the Crime_Rate column , so that my resulting dataframe should look like

City   Crime_Rate

A      10

B      20 

C      20

D      15

I tried

df['Crime_Rate'].replace([np.inf],max(df['Crime_Rate']),inplace=True)

But python takes inf as the maximum value , where am I going wrong here ?

like image 509
Ahamed Moosa Avatar asked Dec 14 '22 16:12

Ahamed Moosa


2 Answers

Filter out inf values first and then get max of Series:

m = df.loc[df['Crime_Rate'] != np.inf, 'Crime_Rate'].max()
df['Crime_Rate'].replace(np.inf,m,inplace=True)

Another solution:

mask = df['Crime_Rate'] != np.inf
df.loc[~mask, 'Crime_Rate'] = df.loc[mask, 'Crime_Rate'].max()

print (df)
  City  Crime_Rate
0    A        10.0
1    B        20.0
2    C        20.0
3    D        15.0
like image 134
jezrael Avatar answered May 16 '23 07:05

jezrael


Here is a solution for a whole matrix/data frame:

highest_non_inf = df.max().loc[lambda v: v<np.Inf].max() df.replace(np.Inf, highest_non_inf)

like image 33
dmeu Avatar answered May 16 '23 07:05

dmeu