Search code examples
pythondataframecsvmeaninfinity

How to avoid infinity when doing calculations python csv


So I am working on a code where I take values from the csv file and multiply them with some numbers. Some of the values in the data are infinity so when I am calculating the mean of that column it is giving me the answer in infinity which make sense. Is there a way I can avoid calculating the that cell that has infinity in it??

I tried using this but it didn't work. Can someone tell me if I am doing this correctly?

cop_average = df["COP"].replace('inf', np.nan).mean(skipna=True)

After running this I am still getting "inf" in the some cells!!


Solution

  • Instead of replacing a string 'inf', you should replace the floating point representation of infinity.

    import pandas as pd
    import numpy as np
    
    
    d = {"COP": [1, 2, np.Inf], "col2": [3, 4, 5]}
    df = pd.DataFrame(data=d)
    
    df["COP"].replace(np.Inf, np.nan).mean(skipna=True)