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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With