我正在尝试将一维numpy数组转换为熊猫数据框。这是我的数组的样子:
myarray
array(['HG00096', '0.0', '0.042134833', ..., '0.011235955', '0.0', ''],
dtype='<U12')
myarray.shape
(6494853,)
我想从此1D数组中获取每个n个元素,并将它们作为一行插入到数据框中。我用myarray中元素的索引填充了“ itemindex”元组,我希望将其用作未来数据帧行的第一个元素。我还生成了一个具有所需尺寸的数据框:
df = pd.DataFrame(index=myarray[itemindex[1]], columns=myarray[itemindex[1]])
但是,当我运行以下代码时,会收到错误消息:
newdf = df.loc[individuals[i]]=(myarray[itemindex[1][i]+1:itemindex[1][i+1]])
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/home/me/venv/lib/python3.6/site-packages/pandas/core/indexing.py", line 190, in __setitem__
self._setitem_with_indexer(indexer, value)
File "/home/me/venv/lib/python3.6/site-packages/pandas/core/indexing.py", line 656, in _setitem_with_indexer
value=value)
File "/home/me/venv/lib/python3.6/site-packages/pandas/core/internals/managers.py", line 510, in setitem
return self.apply('setitem', **kwargs)
File "/home/me/venv/lib/python3.6/site-packages/pandas/core/internals/managers.py", line 395, in apply
applied = getattr(b, f)(**kwargs)
File "/home/me/venv/lib/python3.6/site-packages/pandas/core/internals/blocks.py", line 872, in setitem
check_setitem_lengths(indexer, value, values)
File "/home/me/venv/lib/python3.6/site-packages/pandas/core/indexing.py", line 2534, in check_setitem_lengths
raise ValueError("cannot set using a list-like indexer "
ValueError: cannot set using a list-like indexer with a different length than the value
我找到了有关此问题的其他几篇文章,但是没有一种解决方案对我有用。我想知道如何用数组中的值填充此数据框。
非常感谢, 欢呼声-Gokberk