python - Unable to unpack elements in list -
here's mwe
of code:
import numpy np # load data file. data = np.genfromtxt('data_input', dtype=none, unpack=true) print data
here's sample of data_input
file:
01_500_aa_1000 990.0 990.0 112.5 0.2 72 0 0 1 0 0 0 0 0 0 0 0 0 1 02_500_aa_0950 990.0 990.0 112.5 0.2 77 0 0 1 0 0 0 0 0 0 0 0 0 1 03_500_aa_0600 990.0 990.0 112.5 0.18 84 0 0 1 0 0 0 0 0 0 0 0 0 1 04_500_aa_0700 990.0 990.0 112.5 0.18 84 0 0 1 0 0 0 0 0 0 0 0 0 1
the unpack
argument not appear work since prints:
[ ('01_500_aa_1000', 990.0, 990.0, 112.5, 0.2, 72, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1) ('02_500_aa_0950', 990.0, 990.0, 112.5, 0.2, 77, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1) ('03_500_aa_0600', 990.0, 990.0, 112.5, 0.18, 84, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1) ('04_500_aa_0700', 990.0, 990.0, 112.5, 0.18, 84, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)]
can reproduce this? doing wrong?
you're getting because genfromtxt
returning numpy record array, not list
. it's when print()
console looks list
.
from cstringio import stringio raw = """01_500_aa_1000 990.0 990.0 112.5 0.2 72 0 0 1 0 0 0 0 0 0 0 0 0 1 02_500_aa_0950 990.0 990.0 112.5 0.2 77 0 0 1 0 0 0 0 0 0 0 0 0 1 03_500_aa_0600 990.0 990.0 112.5 0.18 84 0 0 1 0 0 0 0 0 0 0 0 0 1 04_500_aa_0700 990.0 990.0 112.5 0.18 84 0 0 1 0 0 0 0 0 0 0 0 0 1""" sio = stringio(raw) data = genfromtxt(sio, dtype=none, unpack=false) print data print print data.dtype
gives:
[ ('01_500_aa_1000', 990.0, 990.0, 112.5, 0.2, 72, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1) ('02_500_aa_0950', 990.0, 990.0, 112.5, 0.2, 77, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1) ('03_500_aa_0600', 990.0, 990.0, 112.5, 0.18, 84, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1) ('04_500_aa_0700', 990.0, 990.0, 112.5, 0.18, 84, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)] [('f0', 's14'), ('f1', '<f8'), ('f2', '<f8'), ('f3', '<f8'), ('f4', '<f8'), ('f5', '<i8'), ('f6', '<i8'), ('f7', '<i8'), ('f8', '<i8'), ('f9', '<i8'), ('f10', '<i8'), ('f11', '<i8'), ('f12', '<i8'), ('f13', '<i8'), ('f14', '<i8'), ('f15', '<i8'), ('f16', '<i8'), ('f17', '<i8'), ('f18', '<i8')]
unpack=true
, unpack=false
appear return same thing because need recarray
. suggest try pandas
, forget recarray
s altogether. can pass recarray
pandas.dataframe
, get s*** done! example,
df = dataframe(data) print df print print df.f0
yields:
f0 f1 f2 f3 f4 f5 f6 f7 f8 \ 0 01_500_aa_1000 990.00 990.00 112.50 0.20 72 0 0 1 1 02_500_aa_0950 990.00 990.00 112.50 0.20 77 0 0 1 2 03_500_aa_0600 990.00 990.00 112.50 0.18 84 0 0 1 3 04_500_aa_0700 990.00 990.00 112.50 0.18 84 0 0 1 f9 f10 f11 f12 f13 f14 f15 f16 f17 f18 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0 1 3 0 0 0 0 0 0 0 0 0 1 0 01_500_aa_1000 1 02_500_aa_0950 2 03_500_aa_0600 3 04_500_aa_0700 name: f0, dtype: object
Comments
Post a Comment