Reputation: 213
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
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
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
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