简体   繁体   中英

Convert dict with variable length list of dicts to pandas dataframe

I have a dictionary with as values a list of dictionaries and these lists are of variable length. I have tried a lot, but can't get the data properly transformed to a Pandas dataframe.

The data looks like this:

{key1: [{'column5': 40, 'column1': 1, 'column2': 6, 'column3': 170, 'column4': 300}], 
key2: [{'column5': 6, 'column1': 33, 'column2': 5, 'column3': 76, 'column4': 13}], 
key3: [{'column5': 7, 'column1': 44, 'column2': 2, 'column3': 67, 'column4': 13}, {'column5': 45, 'column1': 400, 'column2': 100, 'column3': 12, 'column4': 145}]}

I want to get a frame like this:

      column1  column2 column3   ..
key1  1        6       170
key2  33       5       76
key3  33       2       67
key3  400      100     12
 .
 .

I either get errors like 'Arrays must all be of same length' when using pd.DataFrame.from_records and when using orient=index, the data is still as a dictionary placed in the dataframe. Some of the things I've tried:

df = pd.DataFrame.from_dict(a, orient='index')
df.transpose() //Data is not properly placed in the dataframe

df = pd.DataFrame.from_records(dataset, orient='index') //Data is not properly placed in the dataframe

df = pd.DataFrame.from_records(dataset) //Gives error about length of arrays

df = pd.DataFrame.from_dict(dataset).T //Gives error about length of arrays

How should I go about this? Thanks a lot!

Setup

dct = {'key1': [{'column5': 40, 'column1': 1, 'column2': 6, 'column3': 170, 'column4': 300}],'key2': [{'column5': 6, 'column1': 33, 'column2': 5, 'column3': 76, 'column4': 13}],'key3': [{'column5': 7, 'column1': 44, 'column2': 2, 'column3': 67, 'column4': 13}, {'column5': 45, 'column1': 400, 'column2': 100, 'column3': 12, 'column4': 145}]}

Using a list comprehension to slightly restructure your input dataset:

pd.DataFrame([{'key': k, **i} for  k, v in dct.items() for i in v])

   column1  column2  column3  column4  column5   key
0        1        6      170      300       40  key1
1       33        5       76       13        6  key2
2       44        2       67       13        7  key3
3      400      100       12      145       45  key3

To help better understand why this works, here is the list of dictionaries created by the list comprehension:

[{'key': 'key1', 'column5': 40, 'column1': 1, 'column2': 6, 'column3': 170, 'column4': 300}, {'key': 'key2', 'column5': 6, 'column1': 33, 'column2': 5, 'column3': 76, 'column4': 13}, {'key': 'key3', 'column5': 7, 'column1': 44, 'column2': 2, 'column3': 67, 'column4': 13}, {'key': 'key3', 'column5': 45, 'column1': 400, 'column2': 100, 'column3': 12, 'column4': 145}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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