Convert list of dictionaries to a pandas DataFrame

appleLover picture appleLover · Dec 17, 2013 · Viewed 382.3k times · Source

I have a list of dictionaries like this:

[{'points': 50, 'time': '5:00', 'year': 2010}, 
{'points': 25, 'time': '6:00', 'month': "february"}, 
{'points':90, 'time': '9:00', 'month': 'january'}, 
{'points_h1':20, 'month': 'june'}]

And I want to turn this into a pandas DataFrame like this:

      month  points  points_h1  time  year
0       NaN      50        NaN  5:00  2010
1  february      25        NaN  6:00   NaN
2   january      90        NaN  9:00   NaN
3      june     NaN         20   NaN   NaN

Note: Order of the columns does not matter.

How can I turn the list of dictionaries into a pandas DataFrame as shown above?

Answer

joris picture joris · Dec 17, 2013

Supposing d is your list of dicts, simply:

df = pd.DataFrame(d)

Note: this does not work with nested data.