我有两个列表,我想创建一个包含3列的pandas Dataframe,其中一列包含一个通过压缩列表中的两个而生成的列。我尝试了以下
import pandas as pd
import numpy as np
S_x = [80, 90, 100, 200, 300, 600, 800, 900, 1000, 1200]
S_y = [800, 1000, 1200, 450, 80, 100, 60, 300, 700, 900]
S_z=list(zip(S_x,S_y))
frame4 = pd.DataFrame(np.column_stack([S_x, S_y,S_z]), columns=["Recovered Data", "Percentage Error","Zipped"])
在S_z列中,我希望元素为元组,就像它们在列表S_z中显示的一样,而前两列应该是它们的样子。运行代码时出现错误
ValueError: Shape of passed values is (4, 10), indices imply (3, 10)
我不知道我在做什么错。我正在使用Python 3.x
当您使用np.column_stack时,它会自动解压缩S_z并因此np.column_stack([S_x, S_y,S_z])
变成形状(10,4 )。这样做:
frame4 = pd.DataFrame({"Recovered Data": S_x, "Percentage Error": S_y,"Zipped": S_z})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句