8

I have a basic table of values:

import pandas as pd
import numpy as np
test = pd.read_csv('mean_test.csv')
test.replace('n/a',np.nan)
test


value1  value2  value3
1   9   5
5   NaN 4
9   55  NaN
NaN 4   9

I want to work out the average of the three values, ignoring NaN, so for the second row it would be (5+4)/2. Therefore I can't use the .replace function to put a zero in NaN's place. I have searched through some other questions, but can't find anything that covers this. Am I missing something obvious?

1 Answer 1

13

Pandas takes care of the NaN for you:

>>> df
value1  value2  value3
0       1       9       5
1       5     NaN       4
2       9      55     NaN
3     NaN       4       9

>>> df.mean(axis=1)
0     5.0
1     4.5
2    32.0
3     6.5
dtype: float64
Sign up to request clarification or add additional context in comments.

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.