Combining 3 Arrays into 1 Matrix (Python 3)

I have 3 arrays of equal length (e.g.):

  1. [a, b, c]
  2. [1, 2, 3]
  3. [i, ii, iii]

I would like to combine them into a matrix:

|a, 1, i  |
|b, 2, ii |
|c, 3, iii|

The problem I have is that when I use codes such as dstack, hstack or concatenate. I get them numerically added or stacked in a fashion that I can work with.

Upvotes: 1

Views: 8133

Answers (4)

DirtyBit
DirtyBit

Reputation: 16772

You could use zip():

which maps the similar index of multiple containers so that they can be used just using as single entity.

a1 = ['a', 'b', 'c']

b1 = ['1', '2', '3']

c1 =  ['i', 'ii', 'iii']


print(list(zip(a1,b1,c1)))

OUTPUT:

[('a', '1', 'i'), ('b', '2', 'ii'), ('c', '3', 'iii')]

EDIT:

I just thought of stepping forward, how about flattening the list afterwards and then use numpy.reshape

flattened_list = []

#flatten the list
for x in res:
    for y in x:
        flattened_list.append(y)

#print(flattened_list)

import numpy as np
data = np.array(flattened_list)
shape = (3, 3)
print(data.reshape( shape ))

OUTPUT:

[['a' '1' 'i']
 ['b' '2' 'ii']
 ['c' '3' 'iii']]

OR

for one liners out there:

#flatten the list
for x in res:
    for y in x:
        flattened_list.append(y)

# print(flattened_list)

print([flattened_list[i:i+3] for i in range(0, len(flattened_list), 3)])

OUTPUT:

[['a', '1', 'i'], ['b', '2', 'ii'], ['c', '3', 'iii']]

OR

As suggested by @norok2

print(list(zip(*zip(a1, b1, c1))))

OUTPUT:

[('a', 'b', 'c'), ('1', '2', '3'), ('i', 'ii', 'iii')]

Upvotes: 2

ajrwhite
ajrwhite

Reputation: 8458

If you have different data types in each array, then it would make sense to use pandas for this:

# Iterative approach, using concat
import pandas as pd
my_arrays = [['a', 'b', 'c'], [1, 2, 3], ['i', 'ii', 'iii']]
df1 = pd.concat([pd.Series(array) for array in my_arrays], axis=1)

# Named arrays
array1 = ['a', 'b', 'c']
array2 = [1, 2, 3]
array3 = ['i', 'ii', 'iii']
df2 = pd.DataFrame({'col1': array1,
                    'col2': array2,
                    'col3': array3})

Now you have the structure you desired, with appropriate data types for each column:

print(df1)
#    0  1    2
# 0  a  1    i
# 1  b  2   ii
# 2  c  3  iii

print(df2)
#   col1  col2 col3
# 0    a     1    i
# 1    b     2   ii
# 2    c     3  iii

print(df1.dtypes)
# 0    object
# 1     int64
# 2    object
# dtype: object

print(df2.dtypes)
# col1    object
# col2     int64
# col3    object
# dtype: object

You can extract the numpy array with the .values attribute:

df1.values
# array([['a', 1, 'i'],
#        ['b', 2, 'ii'],
#        ['c', 3, 'iii']], dtype=object)

Upvotes: 0

Hannes Ovrén
Hannes Ovrén

Reputation: 21831

Assuming that you have 3 numpy arrays:

>>> a, b, c = np.random.randint(0, 9, 9).reshape(3, 3)
>>> print(a, b, c)
[4 1 4] [5 8 5] [3 0 2]

then you can stack them vertically (i.e. along the first dimension), and then transpose the resulting matrix to get the order you need:

>>> np.vstack((a, b, c)).T
array([[4, 5, 3],
       [1, 8, 0],
       [4, 5, 2]])

A slightly more verbose example is to instead stack horizontally, but this requires that your arrays are made into 2D using reshape:

>>> np.hstack((a.reshape(3, 1), b.reshape(3, 1), c.reshape(3, 1)))
array([[4, 5, 3],
       [1, 8, 0],
       [4, 5, 2]])

Upvotes: 1

Benoît P
Benoît P

Reputation: 3265

this gives you a list of tuples, which might not be what you want:

>>> list(zip([1,2,3],[4,5,6],[7,8,9]))
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

this gives you a numpy array:

>>> from numpy import array
>>> array([[1,2,3],[4,5,6],[7,8,9]]).transpose()
array([[1, 4, 7],
       [2, 5, 8],
       [3, 6, 9]])

Upvotes: 0

Related Questions