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])
但是有没有更好的方法来做到这一点。更多'熊猫'的方式?
'df.icol(0)。适用(pd.Series)'不给我在所有你写什么。 –
对不起。 'data = ...'中有一组额外的括号 – numentar