【发布时间】:2018-12-31 07:23:03
【问题描述】:
我从以下代码中获得了一个结构化的 numpy 数组:
data = np.genfromtxt(fname, dtype = None, comments = '#', skip_header=1, usecols=(ucols))
其中第一列是以乱序排列的其余数据集的索引(我希望保留)。 我想将结构化数组转换为 Pandas 数据帧,并将加扰索引作为数据帧的可调用索引。
编辑:
import numpy as np
test = np.array([(45,1,'mars',1,1),(67,1,'pluto',1,1),(12,1,'saturn',1,1)],dtype='i,f,U10,i,f')
创建一个 numpy 结构化数组,调用第一个条目给出:
In [5]: test[0]
Out[5]: (45, 1., 'mars', 1, 1.)
调用整个数组:
In [6]: test
Out[6]:
array([(45, 1., 'mars', 1, 1.), (67, 1., 'pluto', 1, 1.),
(12, 1., 'saturn', 1, 1.)],
dtype=[('f0', '<i4'), ('f1', '<f4'), ('f2', '<U10'), ('f3', '<i4'), ('f4', '<f4')])
我想把这个结构化数组变成一个 pandas 数据框,然后在这个例子中让 45,67,12 成为访问数组“行”中数据的可调用索引。
【问题讨论】:
-
您能否提供minimal, complete, and verifiable example 以及预期输出?
-
好的,我会修改
标签: python python-3.x pandas numpy