0

I have a data frame that has some cells with missing data that has inf instead. e.g:

a       b       c
2       3       4
2       3       inf

I want this result:

2        3       4

Is there a way to use the mean function or find the averages of the entire data frame.

3 Answers 3

1

Let us do it with mask inf to nan

df.mask(np.isinf(df)).mean()
Out[63]: 
a    2.0
b    3.0
c    4.0
dtype: float64
Sign up to request clarification or add additional context in comments.

Comments

1

Here's a solution without NumPy:

df.replace(float("inf"), float("nan")).mean(axis = 0)

You can also replace -inf and any other value:

df.replace([float("inf"), float("-inf")], float("nan")).mean(axis = 0)

Comments

0

numpy.nanmean

np.nanmean(df,axis=0)

You can also replace inf with NaN using numpy

Comments

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.