从嵌套字典列表中获取 pandas 数据框 [英] Getting pandas dataframe from list of nested dictionaries

查看:53
本文介绍了从嵌套字典列表中获取 pandas 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,所以这可能很简单,但是经过一段时间后,我仍无法为我的问题找到一个好的答案.我正在尝试从词典列表中创建Pandas数据框.

I am new to Python so this may be pretty straightforward, but I have not been able to find a good answer for my problem after looking for a while. I am trying to create a Pandas dataframe from a list of dictionaries.

我的嵌套词典列表如下:

My list of nested dictionaries is the following:

my_list = [{0: {'a': '23', 'b': '15', 'c': '5', 'd': '-1'}, 
            1: {'a': '5', 'b': '6', 'c': '7', 'd': '9'}, 
            2: {'a': '9', 'b': '15', 'c': '5', 'd': '7'}}, 
           {0: {'a': '5', 'b': '249', 'c': '92', 'd': '-4'}, 
            1: {'a': '51', 'b': '5', 'c': '34', 'd': '1'}, 
            2: {'a': '3', 'b': '8', 'c': '3', 'd': '11'}}]

因此,主词典中的每个键都有3个值.

So each key in the main dictionaries has 3 values.

使用 data = pd.DataFrame(my_list)将其放入数据框会返回不可用的内容,因为每个单元格中都包含有关a,b,c和d的信息.

Putting these into a dataframe using data = pd.DataFrame(my_list) returns something unusable, as each cell has information on a, b, c and d in it.

我想得到一个看起来像这样的数据框:

I want to end up with a dataframe that looks like this:

 name| a  | b  | c | d 
0    | 23 | 15 | 5 | -1 
1    | 5  | 6  | 7 |  9 
2    | 9  | 15 | 5 |  7 
0    | 5  |249 | 92| -4 
1    |51  | 5  | 34|  1 
2    | 3  | 8  | 3 | 11 

这可能吗?

推荐答案

简单:

pd.concat([pd.DataFrame(l) for l in my_list],axis=1).T

这篇关于从嵌套字典列表中获取 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆