Search code examples
pythonpandasreplaceinfinite

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 ?


Solution

  • 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