简体   繁体   中英

Dataframe from list of list of dicts

I searched the website but nothing really matches.

I have an example data:

data_2 = [[{'url':'http://url1.com', 'traffic':810}], [{'url':'http://url2.com', 'traffic':811}]] 

How to create a dataframe from that so that the output would look like this:


                             url traffic
0                 http://url1.com  810
1                 http://url2.com  811

I tried:

import pandas as pd

data_2 = [[{'url':'http://url1.com', 'traffic':810}], [{'url':'http://url2.com', 'traffic':811}]] 

df2 = pd.DataFrame(data = data_2, columns = ['url', 'traffic'])
print(df2)


but received a "ValueError: 2 columns passed, passed data had 1 columns"

You can flatten data_2 then construct a DataFrame with it. One way to flatten it is to use itertools.chain :

from itertools import chain
df = pd.DataFrame(chain.from_iterable(data_2))

or you can construct a Series with data_2 , explode it, convert the outcome to a list, then pass it to a DF constructor:

df = pd.DataFrame(pd.Series(data_2).explode().tolist())

Output:

               url  traffic
0  http://url1.com      810
1  http://url2.com      811

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