Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read JSON file that contains list of dictionaries into pandas data frame?

I've got a list of dictionaries in a JSON file that looks like this:

[{"url": "http://www.URL1.com", "date": "2001-01-01"}, 
 {"url": "http://www.URL2.com", "date": "2001-01-02"}, ...]

but I'm struggling to import it into a pandas data frame — this should be pretty easy, but I'm blanking on it. Anyone able to set me straight here?

Likewise, what's the best way to simply read it into a list of dictionaries to use w/in python?

like image 881
scrollex Avatar asked Jan 10 '16 17:01

scrollex


1 Answers

You can use from_dict:

import pandas as pd

lis = [{"url": "http://www.URL1.com", "date": "2001-01-01"}, 
       {"url": "http://www.URL2.com", "date": "2001-01-02"}]

print pd.DataFrame.from_dict(lis)

         date                  url
0  2001-01-01  http://www.URL1.com
1  2001-01-02  http://www.URL2.com

Or you can use DataFrame constructor:

import pandas as pd

lis = [{"url": "http://www.URL1.com", "date": "2001-01-01"}, {"url": "http://www.URL2.com", "date": "2001-01-02"}]

print pd.DataFrame(lis)

         date                  url
0  2001-01-01  http://www.URL1.com
1  2001-01-02  http://www.URL2.com
like image 123
jezrael Avatar answered Sep 30 '22 14:09

jezrael