I have array(['USD/EUR', 'nan'], dtype='<U32') that I want to split so that I get [USD, EUR]. How can this be done?
The .split function is not working for me.
CodePudding user response:
If you only care about the element 'USD/EUR', then just extract it, then use split(), specifying that the split should be on the '/' character:
import numpy as np
a = np.array(['USD/EUR', 'nan'], dtype='<U32')
b = a[0].split('/')
print(b)
CodePudding user response:
np.char module has a bunch of functions that apply string methods to a U dtype array. It's not faster than doing the equivalent list comprehension, but may be more convenient.
In [19]: arr = np.array(['USD/EUR', 'nan'])
In [20]: arr
Out[20]: array(['USD/EUR', 'nan'], dtype='<U7')
In [21]: np.char.split(arr, '/')
Out[21]: array([list(['USD', 'EUR']), list(['nan'])], dtype=object)
