I have this array:
z = np.array(['43', '65', '41', '47', '46', '73', '99', '52', '56', '23', '07',
'C3', '49', '62', '54', 'A1', '88', '70', '42', 74.0, 20.0, 21.0,
4, 62, 2, 3, 49, 79, '13', 'F4', 'A9', '20', '19', 19.0, 23.0,
70.0, 83, 61, 80, 81, 66, 82, 63, '09', '06', 'F8'], dtype=object)
In this array, we have int, str and float in one array. I want to convert all of them into string but float values have to be integer and the values such as '07', '09', etc also turn into '7', '9'. The desire result I want is:
z = np.array(['43', '65', '41', '47', '46', '73', '99', '52', '56', '23', '7',
'C3', '49', '62', '54', 'A1', '88', '70', '42', '74', '20', '21',
'4', '62', '2', '3', '49', '79', '13', 'F4', 'A9', '20', '19', '19', '23',
'70', '83', '61', '80', '81', '66', '82', '63', '9', '6', 'F8'], dtype=object)
I have tried this method
def col_convert(array):
for i in range(len(array)):
try:
array[i] = str(int(array[i]))
except:
next
return array
However for 1 million elements, this solution are quite slow. Are there any way faster to handle this task?
Try:
z1 = np.array([str(i).split('.')[0] for i in z])
UPDATE: Per OP's edit to remove leading zeros:
z1 = np.array([str(i).lstrip('0').split('.')[0] for i in z])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With