Skip to content Skip to sidebar Skip to footer

Replace Character In Numpy Ndarray (python)

I have a numpy ndarray with 6 elements: ['\tblah blah' ''''123' 'blah' ''''' '\t456' '78\t9'] I am trying to replace all tab characters \t with 4 spaces each so that the numpy arr

Solution 1:

You could use NumPy's core.defchararray that deals with string related operations and for this case use replace method, like so -

np.core.defchararray.replace(arr,'\t', '    ')

Sample run -

In [44]: arr
Out[44]: 
array(['\tblah blah', '"""123', 'blah', '"""', '\t456', '78\t9'], 
      dtype='|S10')

In [45]: np.core.defchararray.replace(arr,'\t', '    ')
Out[45]: 
array(['    blah blah', '"""123', 'blah', '"""', '    456', '78    9'], 
      dtype='|S13')

Post a Comment for "Replace Character In Numpy Ndarray (python)"