每个子列表中总共有49个子子列表。我试图将4个子列表放入数据帧但我没有这样做。
我得到的错误是:
4 columns passed, passed data had 49 columns
我应该通过它制作没有子子列表的新列表并将其传递给数据帧吗?还是有其他办法。
这是我尝试过的。
#!/usr/bin/env python
import pandas
import numpy as np
test = [[[32233.982703526675], [32667.454274693522], [33100.925845860365], [33534.39741702721], [33967.86898819406], [34401.340559360906],
[34834.81213052775], [35268.2837016946], [35701.75527286144], [36135.226844028286], [36568.69841519513], [37002.16998636198],
[37435.64155752883], [37869.11312869567], [38302.58469986252], [38736.05627102937], [39169.527842196214], [39602.99941336305],
[40036.4709845299], [40469.94255569675], [40903.414126863594], [41336.88569803044], [41770.35726919729], [42203.828840364135],
[42637.300411530974], [43070.77198269783], [43504.24355386467], [43937.715125031515], [44371.18669619836], [44804.65826736521],
[45238.129838532055], [45671.6014096989], [46105.07298086575], [46538.54455203259], [46972.01612319944], [47405.48769436628],
[47838.95926553313], [48272.430836699976], [48705.90240786682], [49139.37397903367], [49572.84555020051], [50006.31712136736],
[50439.7886925342], [50873.26026370106], [51306.7318348679], [51740.20340603474], [52173.67497720159], [52607.14654836844],
[53040.618119535284]],
[[24931.88896226113], [25322.701814749595], [25713.51466723806], [26104.327519726525], [26495.140372214988], [26885.95322470345],
[27276.766077191915], [27667.57892968038], [28058.391782168845], [28449.204634657308], [28840.01748714577], [29230.830339634238],
[29621.6431921227], [30012.456044611165], [30403.268897099628], [30794.08174958809], [31184.894602076558], [31575.70745456502],
[31966.520307053484], [32357.333159541948], [32748.14601203041], [33138.95886451888], [33529.77171700734], [33920.584569495804],
[34311.397421984264], [34702.21027447273], [35093.0231269612], [35483.83597944966], [35874.648831938124], [36265.461684426584],
[36656.27453691505], [37047.08738940352], [37437.90024189198], [37828.713094380444], [38219.525946868904], [38610.33879935737],
[39001.15165184584], [39391.9645043343], [39782.777356822764], [40173.59020931122], [40564.40306179969], [40955.21591428816],
[41346.028766776624], [41736.841619265084], [42127.65447175354], [42518.46732424201], [42909.28017673048], [43300.093029218944],
[43690.905881707404]],
[[19971.77495538324], [20291.705860786995], [20611.636766190753], [20931.56767159451], [21251.498576998267], [21571.429482402025],
[21891.36038780578], [22211.29129320954], [22531.222198613294], [22851.153104017052], [23171.084009420807], [23491.014914824565],
[23810.94582022832], [24130.87672563208], [24450.807631035837], [24770.738536439592], [25090.66944184335], [25410.600347247106],
[25730.531252650864], [26050.462158054623], [26370.393063458378], [26690.323968862136], [27010.25487426589], [27330.18577966965],
[27650.116685073408], [27970.047590477163], [28289.978495880918], [28609.909401284676], [28929.840306688435], [29249.771212092193],
[29569.70211749595], [29889.633022899703], [30209.56392830346], [30529.49483370722], [30849.425739110975], [31169.356644514733],
[31489.28754991849], [31809.218455322247], [32129.149360726005], [32449.08026612976], [32769.011171533515], [33088.942076937274],
[33408.87298234103], [33728.80388774479], [34048.73479314854], [34368.6656985523], [34688.59660395606], [35008.52750935982],
[35328.458414763576]],
[[14607.094998177201], [14835.960593646389], [15064.826189115578], [15293.691784584766], [15522.557380053953], [15751.42297552314],
[15980.288570992328], [16209.154166461518], [16438.019761930707], [16666.885357399893], [16895.750952869083], [17124.61654833827],
[17353.482143807458], [17582.347739276644], [17811.213334745833], [18040.078930215022], [18268.94452568421], [18497.810121153398],
[18726.675716622587], [18955.541312091773], [19184.406907560962], [19413.27250303015], [19642.138098499338], [19871.003693968523],
[20099.869289437713], [20328.734884906902], [20557.600480376088], [20786.466075845277], [21015.331671314467], [21244.197266783653],
[21473.062862252842], [21701.92845772203], [21930.794053191217], [22159.659648660407], [22388.525244129592], [22617.390839598782],
[22846.256435067968], [23075.122030537157], [23303.987626006347], [23532.853221475532], [23761.71881694472], [23990.58441241391],
[24219.450007883097], [24448.315603352286], [24677.181198821476], [24906.04679429066], [25134.912389759847], [25363.777985229037],
[25592.643580698226]]]
foo = pandas.DataFrame(test, columns=['24','22', '20' ,'18'])
print foo
感谢。
答案 0 :(得分:1)
这就是你想要的吗?
from itertools import chain
foo = pandas.DataFrame(
zip(*[chain(*col) for col in test]),
columns=['24','22', '20' ,'18']
)
修改强>
使用numpy.reshape
pandas.DataFrame(
np.reshape(np.array(test).T, (49, 4), order='C'),
columns=['24','22', '20' ,'18']
)