Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


                             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"

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading