将元组列表转换为数据框并在python中转置

时间:2018-11-01 21:32:47

标签: python pandas dataframe tuples

我想将元组列表转换为如下所示的pandas数据框。我想将元组转换为熊猫数据框并进行转置。

data = {'Document_No':[0.0,1.0,2.0,3.0,4.0], 'list_of_topics':[[(0, 0.039169993), (1, 0.023344912)],[(0, 0.17865846), (1, 0.01093025)],[(0, 0.039170124), (1, 0.023344917)],  [(0, 0.17865846), (1, 0.01093025)], [(0, 0.039170124), (1, 0.023344917)]]}
df = pd.DataFrame(data=data)

   Document_No            list_of_topics
0  0.0                [(0, 0.039169993), (1, 0.023344912)]
1  1.0                [(0, 0.17865846), (1, 0.01093025)]
2  2.0                 [(0, 0.039170124), (1, 0.023344917)]
3  3.0                [(0, 0.17865846), (1, 0.01093025)]
4  4.0                 [(0, 0.039170124), (1, 0.023344917)]


data = {'0':[0.039169993,0.023344912], '1':[0.17865846,0.01093025],'2':[0.039170124,0.023344917], '3':[0.17865846,0.01093025],'4':[0.039170124,0.023344917]}
desired_result= pd.DataFrame(data)


         0.0            1.0          2.0        3.0          4.0
0  0.039169993   0.17865846  0.039170124   0.17865846  0.039170124
1  0.023344912   0.01093025  0.023344917   0.01093025  0.023344917

2 个答案:

答案 0 :(得分:3)

您可以使用列表理解功能进行一些预处理,然后将其传递给DataFrame构造函数:

df = pd.DataFrame([[j[1] for j in i] for i in data['list_of_topics']], index=data['Document_No']).transpose()

收益:

        0.0       1.0       2.0       3.0       4.0
0  0.039170  0.178658  0.039170  0.178658  0.039170
1  0.023345  0.010930  0.023345  0.010930  0.023345

答案 1 :(得分:0)

与@ rahlf23类似的方式,没有列表理解,其工作原理是将list_of_topics转换为字典结构:

>>> pd.DataFrame(list(map(dict,df.list_of_topics.tolist())),index=data['Document_No']).T
        0.0       1.0       2.0       3.0       4.0
0  0.039170  0.178658  0.039170  0.178658  0.039170
1  0.023345  0.010930  0.023345  0.010930  0.023345