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

4
  • 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? Commented Sep 27, 2021 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 Commented Sep 27, 2021 at 13:17
  • Check it anyway. You might learn something new :) Commented Sep 27, 2021 at 13:22
  • just filter it similar to stackoverflow.com/a/55228059/14237276 before mean df there would be your df["COP"] Commented Sep 27, 2021 at 13:31

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.