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.

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'
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.