python

Convert a dictionary to pandas DataFrame in python

To convert a python dictionary to a pandas DataFrame, the from_dict() of DataFrame can be used.

import pandas as pd

my_dict = [{'Name': 'John', 'Score': 20}, {'Name': 'Mark', 'Score': 50}, {'Name': 'Rick', 'Score': 70}]

df = pd.DataFrame.from_dict(my_dict)
print(df)

#   +----+--------+---------+
#   |    |  Name  |  Score  |
#   +-------------+---------+
#   | 0  |  John  |     20  |
#   | 1  |  Mark  |     50  |
#   | 2  |  Rick  |     70  |
#   +----+--------+---------+

In the code snippet, we have created a list of dictionary my_dict which has two keys in each dictionary Name and Score. We are using the from_dict() method which is consuming this dictionary and making a DataFrame from it.

Was this helpful?