简体   繁体   English

将字典列表转换为 dataframe python

[英]Convert list of dictionaries into dataframe python

I have a list that i would like to convert to dataframe. I have around 30000 lists in a variable called data.我有一个列表,我想将其转换为 dataframe。我在一个名为数据的变量中有大约 30000 个列表。 how do i convert this into a dataframe with columns properties, product_id,description,ustomer_id and country.我如何将其转换为具有列属性、product_id、描述、ustomer_id 和国家/地区的 dataframe。 I want the element properties to be converted to a dataframe我希望将元素属性转换为 dataframe

data[0]
Out[16]: 
 {'event': 'Product',
     'properties': {'invoice_no': '44',
      'product_id': '67',
      'description': 'cloth',
      'customer_id': 55,
      'country': 'US'}}


data[1]
 Out[17]: 
    {'event': 'Product',
     'properties': {'invoice_no': '55',
      'product_id': '66',
      'description': 'shoe',
      'customer_id': 23,
      'country': 'China'}}

Tried this,试过这个,

new = pd.DataFrame.from_dict(data)

but it gave only two columns such as 'event' and 'properties'.但它只给出了两列,例如“事件”和“属性”。 I want properties to form a dataframe我希望属性形成一个 dataframe

Using your small example set:使用您的小示例集:

>>> from pprint import pprint
>>> pprint(data)
[{'event': 'Product',
  'properties': {'country': 'US',
                 'customer_id': 55,
                 'description': 'cloth',
                 'invoice_no': '44',
                 'product_id': '67'}},
 {'event': 'Product',
  'properties': {'country': 'China',
                 'customer_id': 23,
                 'description': 'shoe',
                 'invoice_no': '55',
                 'product_id': '66'}}]

You can simply use a generator expression to munge your dict into an appropriate form:您可以简单地使用生成器表达式将您的 dict 变成适当的形式:

>>> pd.DataFrame(d['properties'] for d in data)
  country  customer_id description invoice_no product_id
0      US           55       cloth         44         67
1   China           23        shoe         55         66

You can also do:你也可以这样做:

from pandas.io.json import json_normalize
import pandas as pd
resultDf = pd.DataFrame()

for dictionary in data:
    for key, value in dictionary.items():

        if key == 'properties':
            df = json_normalize(value)
            resultDf = resultDf.append(df)

print(resultDf) gives: print(resultDf)给出:

  country  customer_id description invoice_no product_id
0      US           55       cloth         44         67
1   China           23        shoe         55         66

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM