4

I'm loading data from a csv using loadtxt where all values are floats with the exception of missing data which is coded as the character "?".

I'm trying to create a masked array such that I can use np.ma functions on the loaded data where the missing data will be ignored for the purpose of averages, etc. I've read the documentation for masked_array and this is probably incredibly trivial but I can't seem to figure out how to mask the array such that ? are ignored for the purpose of np.ma mathematical functions.

1 Answer 1

3

You can simply use np.genfromtxt() to read the files and mask the resulting nan values. For example:

input:

11, 12, 13, ?, ?, 16
21, 22, ?, 24, ?, 26

code:

a = np.genfromtxt('test.txt', delimiter=',', missing_values='?', usemask=True)

a.sum(axis=1).data
#array([ 52.,  93.])

a.mean()
#18.125
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.