2015-06-26 180 views
0

我有一個數據框,其中每個元素是元組列表。拆分熊貓數據框列中的元組列表

import pandas as pd 
data={'A':[[('potatoes',9),('cabbages',7),('carrots',5),('onions',2)]], 
     'B':[[('cabbages',5),('apples',1),('plums',9),('peaches',6)]]} 
df = pd.DataFrame(data).transpose() 
print df 
                0 
A [(potatoes, 9), (cabbages, 7), (carrots, 5), (... 
B [(cabbages, 5), (apples, 1), (plums, 9), (peac... 

我希望把它分解到各含元組只是元素的數據框:

ww = df.icol(0).apply(pd.Series) 
print ww 

   0    1    2    3 
A (potatoes, 9) (cabbages, 7) (carrots, 5) (onions, 2) 
B (cabbages, 5) (apples, 1) (plums, 9) (peaches, 6) 

我想去一個級別上的,並有這作爲最終結果:

  0 1   2 3  4 5  6 7 
A potatoes 9 cabbages 7 carrots 5 onions 2 
B cabbages 5 apples 1 plums 9 peaches 6 

我現在通過:

ww2 = pd.concat([ww[col].apply(pd.Series) for col in ww.columns], axis=1) 
ww2.columns = range(ww2.shape[1]) 

但是有沒有更好的方法來做到這一點。更多'熊貓'的方式?

+0

'df.icol(0)。適用(pd.Series)'不給我在所有你寫什麼。 –

+0

對不起。 'data = ...'中有一組額外的括號 – numentar

回答

1

見下文,

ww3 = pd.DataFrame()  
l = len(ww.columns) 
for i in range(l): 
    ww3[i] = ww[i].apply(lambda x: x[0]) 
    ww3[i+l] = ww[i].apply(lambda x: x[1]) 
print (ww3) 

      0 4   1 5  2 6  3 7 
A potatoes 9 cabbages 7 carrots 5 onions 2 
B cabbages 5 apples 1 plums 9 peaches 6