1

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!!

Rookie 18
  • 31
  • 5
  • Seems like you would want to do the replace first, then check the df to see what happened. Did they replace? Or is your replace failing? – Chris Sep 27 '21 at 13:09
  • I think my replace function is working because when I do this `cop_average = df["COP"].replace(0, np.nan).mean(skipna=True)` it works it just doesn't work on infinity for some reason plus I don't even get no error – Rookie 18 Sep 27 '21 at 13:17
  • Check it anyway. You might learn something new :) – Chris Sep 27 '21 at 13:22
  • just filter it similar to https://stackoverflow.com/a/55228059/14237276 before mean df there would be your df["COP"] – Abel Sep 27 '21 at 13:31

1 Answers1

1

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)
ajawa
  • 38
  • 5
  • Did you try my code? It is working with python=3.8.10, pandas=1.3.3, numpy=1.21.2. If not, I need more information about your data. – ajawa Sep 28 '21 at 13:08