2013-11-03 48 views
0

每個子目錄共有49個子子目錄。我試圖將4個子列表合併到一個數據框中,但我失敗了。 我得到的錯誤是: 4 columns passed, passed data had 49 columns將子子目錄傳遞給Pandas Dataframe

我應該通過它使新的列表,而分次列表,並將其傳遞到數據幀?或者還有其他方法。

這是我試過的。

#!/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 

謝謝。

回答

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'] 
) 
+0

是。但正在創建一個非常有效的新列表?可能會有另一種可能嗎? – pistal

+0

您的數據存在問題。首先,當你將數據傳遞給'DataFrame'構造函數時,它期望它以行方式提供。因此'4列通過,通過數據有49列'錯誤。第二個是內部列表的存在。你可以使用' 'foo = pandas.DataFrame.from_dict(dict(zip'['24','22','20','18'],test)))'但我不認爲它給你想要的。使用'zip'和'chain'不是很高效,你可以使用'np.reshape'來獲得更好的結果。你可能會得到比這更好的答案,但我現在無法想到。 – zero323