1

I have the following list

import numpy as np
Y = [np.array([[1, 4, 7],
        [2, 5, 8]]),
 np.array([[10, 14, 18],
        [11, 15, 19],
        [12, 16, 20],
        [13, 17, 21]]),
 np.array([[22, 26, 31],
        [24, 28, 33],
        [26, 30, 35]])]

I want to loop through and print the columns inside of all the arrays in Y.

I don't know how to access the columns of Y. Running Y[:,0] for example, does not give me

[[1]
 [2]]

Instead, it gives me the following error

TypeError: list indices must be integers or slices, not tuple

I want to print all columns of all the arrays in Y, not just the first column of the first array.

Amelia
  • 97
  • 8
  • Is there a reason you are making a list of arrays rather than a single multi-dimensional array like you would get if you did `np.concatenate(Y)` with your current data? You can, of course, do `np.concatenate(Y)[:,0]`, but if would make more sense to start with a structure that supports the thing you want to accomplish. – Mark Nov 07 '22 at 03:18
  • `Y` is a list. It doesn't have columns. The array elements are 2d, and have columns.You have to iterate on the list. – hpaulj Nov 07 '22 at 03:34
  • Yes, my original data contains a list of arrays and I want to operate on its columns without concatenating them. – Amelia Nov 07 '22 at 04:24

3 Answers3

1

Does this help?

for i in range(3):
    l = Y[i]
    for j in range(len(np.transpose(l))):
        print(l[:,j])

This gives you:

[1 2]
[4 5]
[7 8]
[10 11 12 13]
[14 15 16 17]
[18 19 20 21]
[22 24 26]
[26 28 30]
[31 33 35]
S C
  • 284
  • 2
  • 9
1

Slight variation of SC's answer:

for array in Y:
    for row in array.T:
        print(row)

returns:

[1 2]
[4 5]
[7 8]
[10 11 12 13]
[14 15 16 17]
[18 19 20 21]
[22 24 26]
[26 28 30]
[31 33 35]

... using the fact that for iterates over rows of an array. (.T just transposes the array, so columns become rows)

isCzech
  • 313
  • 1
  • 1
  • 7
0

You could use a DataFrame as a higher level structure instead of a list:

import pandas as pd
df = pd.concat(map(pd.DataFrame, Y), keys=range(len(Y)))

df.loc[(0,), 0]

output:

0    1
1    2
Name: 0, dtype: int64

df:

      0   1   2
0 0   1   4   7
  1   2   5   8
1 0  10  14  18
  1  11  15  19
  2  12  16  20
  3  13  17  21
2 0  22  26  31
  1  24  28  33
  2  26  30  35

Other option if you don't need the second index level:

df2 = pd.concat(map(pd.DataFrame, Y), keys=range(len(Y))).droplevel(1)

df2.loc[0, 0]

output:

0    1
0    2
Name: 0, dtype: int64

df2:

    0   1   2
0   1   4   7
0   2   5   8
1  10  14  18
1  11  15  19
1  12  16  20
1  13  17  21
2  22  26  31
2  24  28  33
2  26  30  35
mozway
  • 194,879
  • 13
  • 39
  • 75
  • My original data is in the form of a list, and there are other algorithms that I need to perform on it, as a list, so I won't be able to use dataframes. – Amelia Nov 07 '22 at 17:38