重新排列字典列表以在数据框python中创建列

问题描述 投票:0回答:1

我有一个字典列表,需要将其转换为数据框。特别需要发生的是,列表中每个字典中的第二个键的值应成为列标题,而字典列表中第一个键的值应成为每个列的值。

test_list = [{"key" : 1, "code" : "update"}, 
             {"key" : 2, "code" : "birthday"}, 
             {"key" : 3, "code" : "today"}] 

数据帧输出应如下所示:

df示例输出:

update birthday today
1      2        3

我曾考虑过制作两个列表,然后将它们压缩到新字典中,但是不确定这是否是最佳方法。任何帮助,将不胜感激。

python pandas list dictionary
1个回答
0
投票

IIUC

s = pd.DataFrame(test_list).set_index('code').T
Out[7]: 
code  update  birthday  today
key        1         2      3
© www.soinside.com 2019 - 2024. All rights reserved.