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.

jm96
  • 57
  • 5

3 Answers3

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
BENY
  • 317,841
  • 20
  • 164
  • 234
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)
Pablo C
  • 4,661
  • 2
  • 8
  • 24
0

numpy.nanmean

np.nanmean(df,axis=0)

You can also replace inf with NaN using numpy

Jon S
  • 165
  • 6