将字典列表转换为数据框 [英] Convert list of Dictionaries to a Dataframe

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

问题描述

我面临着一个基本的问题,即转换通过解析具有json格式的文本的列而获得的字典列表.以下是数据的简要快照:

I am facing a basic problem of converting a list of dictionaries obtained from parsing a column with text in json format. Below is the brief snapshot of data:

[{u'PAGE TYPE': u'used-serp.model.brand.city'},
 {u'BODY TYPE': u'MPV Cars',
  u'ENGINE CAPACITY': u'1461',
  u'FUEL TYPE': u' Diesel',
  u'MODEL NAME': u'Renault Lodgy',
  u'OEM NAME': u'Renault',
  u'PAGE TYPE': u'New-ModelPage.OverviewTab'},
 {u'PAGE TYPE': u'used-serp.brand.city'},
 {u'BODY TYPE': u'SUV Cars',
  u'ENGINE CAPACITY': u'2477',
  u'FUEL TYPE': u' Diesel',
  u'MODEL NAME': u'Mitsubishi Pajero',
  u'OEM NAME': u'Mitsubishi',
  u'PAGE TYPE': u'New-ModelPage.OverviewTab'},
 {u'BODY TYPE': u'Hatchback Cars',
  u'ENGINE CAPACITY': u'1198',
  u'FUEL TYPE': u' Petrol , Diesel',
  u'MODEL NAME': u'Volkswagen Polo',
  u'OEM NAME': u'Volkswagen',
  u'PAGE TYPE': u'New-ModelPage.GalleryTab'},

此外,我正在解析的代码如下:

Furthermore, the code i am using to parse is detailed below:

stdf_noncookie = []
stdf_noncookiejson = []

for index, row in df_noncookie.iterrows():
    try:
        loop_data = json.loads(row['attributes'])
        stdf_noncookie.append(loop_data)
    except ValueError:
        loop_nondata = row['attributes']
        stdf_noncookiejson.append(loop_nondata)

stdf_noncookie是我要转换为pandas数据框的词典列表. 属性"是带有json格式文本的列.我试图从此链接中获得一些学习,但是这无法实现解决我的问题.将字典列表转换为熊猫数据框的任何建议/提示都将很有帮助.

stdf_noncookie is the list of dictionaries i am trying to convert into a pandas dataframe. 'attributes' is the column with text in json format. I have tried to get some learning from this link, however this was not able to solve my problem. Any suggestion/tips for converting a list of dictionaries to panda dataframe will be helpful.

推荐答案

要将字典列表转换为熊猫数据框,请使用以下命令:

To convert your list of dicts to a pandas dataframe use the following:

stdf_noncookiejson = pd.DataFrame.from_records(data)

pandas.DataFrame .from_records

DataFrame.from_records (数据,索引=无,排除=无,列=无,coerce_float = False,nrows =无)

您可以在

如果您使用json,也可以使用read_json方法

If youre working with json you can also use the read_json method

stdf_noncookiejson = pd.read_json(data)

pandas.read_json

pandas.read_json (path_or_buf =无,orient =无,typ =帧",dtype = True,convert_axes = True,convert_dates = True, keep_default_dates = True,numpy = False,precise_float = False, date_unit = None,编码= None,行= False)

pandas.read_json (path_or_buf=None, orient=None, typ='frame', dtype=True, convert_axes=True, convert_dates=True, keep_default_dates=True, numpy=False, precise_float=False, date_unit=None, encoding=None, lines=False)

这篇关于将字典列表转换为数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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