Reputation: 57
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.
Upvotes: 0
Views: 252
Reputation: 4761
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)
Upvotes: 1
Reputation: 323266
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
Upvotes: 1