Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to group numpy array position values?

There is a np.array:

[
array(['x_0', '2/20/1990', '3/20/1990'], dtype=object), 
array(['x_1', '1', '2'], dtype=object), 
array(['x_3', 'foo', 'bar'], dtype=object), etc...]

I want to make an array that will contain all of this values grouped (all first values with 1-st values, second values with second, and separated with "\n":

like this:

[array(['x_0\nx_1\nx_n, '2/20/1990\n1\nfoo', '3/20/1990\n2\bar', etc], dtype=object)]
like image 769
alohamaloha Avatar asked Dec 28 '25 18:12

alohamaloha


1 Answers

You can use python built-in zip function and join :

>>> a=[
... np.array(['x_0', '2/20/1990', '3/20/1990'], dtype=object), 
... np.array(['x_1', '1', '2'], dtype=object), 
... np.array(['x_3', 'foo', 'bar'], dtype=object)]

>>> new=np.array(['\n'.join(k) for k in zip(*a)],dtype=object)
>>> new
array(['x_0\nx_1\nx_3', '2/20/1990\n1\nfoo', '3/20/1990\n2\nbar'], dtype=object)
like image 166
Mazdak Avatar answered Dec 31 '25 08:12

Mazdak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!