6

I have an array in numpy, which was generated using np.array() from a python list so my entries are strings, but some of the values are blank. Here is an example array:

['1', '1', '1', '1']
['1', '1', '', '1']
['1', '1', '1', '1']
['1', '', '1', '1']

There is no 'NaN' or 'None', it is blank. I want to be able to fill all the blank cells in a particular column with the same value.

Shadow
9,5474 gold badges50 silver badges60 bronze badges
asked Dec 11, 2013 at 6:30
1

1 Answer 1

6

You can use numpy.where() to achieve this.

In [8]: arr = numpy.array(['','1','2','3',''])
In [9]: arr[numpy.where(arr=='')] = '0'
In [10]: arr
Out[10]:
array(['0', '1', '2', '3', '0'],
 dtype='|S1')

Edit As @mgilson pointed out, you could just do:

arr[arr==''] = '0'
answered Dec 11, 2013 at 6:34
Sign up to request clarification or add additional context in comments.

4 Comments

I don't think np.where is necessary here (is it?). can't you just do arr[arr==''] = '0'?
I want to fill the blank cells of a particular column, not all blank cells. Say I use the code: arr[arr[0::,1]==''] = '0'. This turns any row with a blank in the 2nd column into all 0 entries, not just the blank.
You need to index the first data as well. Try: data[:,1][data[:,1]==''] = '0'
Awesome, that worked. Thank you, I tried using numpy.where() before but didn't use the index.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.