embedded_crysis
embedded_crysis

Reputation: 213

Can numpy.genfromtxt() returns list of column data, not row data?

I have a txt file which looks something like

col1 col2 col3
1    4    7
2    5    8
3    6    9

and I am trying to produce a list of lists containing the column data:

[ [1,2,3] [4,5,6] [7,8,9] ]

but the following code produces a list of row data, i.e. [ [1,4,7], ... ].

blah = np.genfromtxt(scrubbed_file, skip_header=1)
for bluh in blah:
    print(bluh)

Is there an easy way to achieve this?

Upvotes: 0

Views: 1931

Answers (3)

Neill Herbst
Neill Herbst

Reputation: 2122

Use the unpack variable from genfromtext:

np.genfromtxt('text.csv', delimiter=',', unpack=True, skip_header=1)

It gives the following:

array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.],
       [ 7.,  8.,  9.]])

Upvotes: 1

embedded_crysis
embedded_crysis

Reputation: 213

There's a very convenient python feature that does exactly this, namely zip(*list):

blah = np.genfromtxt(scrubbed_file, skip_header=1)
for bluh in zip(*blah):
    print(bluh)

Upvotes: 0

omu_negru
omu_negru

Reputation: 4770

you can just transpose the resulting array like so: blah = np.genfromtxt(scrubbed_file, skip_header=1).T This will effectively put the array on it's side, resulting in the data format you want.

Upvotes: 2

Related Questions