我有一个数据帧,其中每个元素都是元组的列表。在
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...
我想把它分解成一个只包含每个元组元素的数据帧:
^{pr2}$给予
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])
但是有没有更好的方法来做这件事呢。更“熊猫”的方式?在
见下文
相关问题 更多 >
编程相关推荐