How to convert a list of dictionaries to a pandas dataframe in python

Let’s say you have a list of dictionaries and you want them to convert into a dataframe in python. Well, this is a straightforward task using the pandas’ library in python.

In this short python tutorial, I will guide you to the different ways of converting a list of dictionaries into a pandas dataframe.

Method No 1: Convert a list of Dictionaries to Pandas dataframe using Dataframe()

Converting a list of dictionaries to pandas dataframe is made easy by using DataFrame() class. To convert a list of dictionaries to a dataframe in python, use the DataFrame() function. IT will convert a list of dictionaries to a dataframe.

Let’s say you have a list of dictionaries with the name d. You can do the following code to convert a list of dictionaries to convert it to the dataframe in python.

See the following coding


import pandas as pd

d = [{'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'}]

df = pd.DataFrame(d)
print(df)

Output:

  points  time    year     month  points_h1
0    50.0  5:00  2010.0       NaN        NaN
1    25.0  6:00     NaN  february        NaN
2    90.0  9:00     NaN   january        NaN
3     NaN   NaN     NaN      june       20.0

It is important to know that, this method works only if there is no listed dictionaries.

Method No 2 : Converting a list of dictionaries to a pandas dataframe using pd.from_dict() function


import pandas as pd

d = [{'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'}]

r2=pd.DataFrame.from_dict(d)
print(r2)

Output of the code


   points  time    year     month  points_h1
0    50.0  5:00  2010.0       NaN        NaN
1    25.0  6:00     NaN  february        NaN
2    90.0  9:00     NaN   january        NaN
3     NaN   NaN     NaN      june       20.0

Method No 3 : Convert a list of dictonaries to dataframe in python using from_records()


import pandas as pd

d = [{'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'}]


r3=pd.DataFrame.from_records(d)


print(r3)

Output of the code


   points  time    year     month  points_h1
0    50.0  5:00  2010.0       NaN        NaN
1    25.0  6:00     NaN  february        NaN
2    90.0  9:00     NaN   january        NaN
3     NaN   NaN     NaN      june       20.0

Summary and Conclusion

All the methods works fine, The aim of this post will be to show examples of these methods under different situations, discuss when to use (and when not to use), and suggest alternatives.

Leave a Comment

Scroll to Top