jm96
jm96

Reputation: 57

Calculate mean of dataframe with missing data

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

Answers (3)

Pablo C
Pablo C

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

BENY
BENY

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

Jon S
Jon S

Reputation: 175

numpy.nanmean

np.nanmean(df,axis=0)

You can also replace inf with NaN using numpy

Upvotes: 0

Related Questions