Menu Close

How to Convert Dictionary to Excel in Python

How to Convert Dictionary to Excel in Python

In this article, you will see everything about how to convert dictionary to excel in Python with the help of examples. Most of the time you will need to convert Python dictionaries or a list of Python dictionaries into excel. Then this article is going to be very helpful for you.

Prerequisites

Before going further into this article you will need to install the following packages using the pip command, If you have already installed then you can ignore them.


pip install pandas
pip install openpyxl

How to convert Dictionary to excel in Python

Here we will see a total of two ways to convert Python dictionaries into excel. The first is converting a single dictionary to excel and the second will be converting a list of dictionaries into excel.

Convert Dictionary to Excel in Python

In this example, we will see how to convert Python dictionary to excel using the Python pandas module.

Example: Convert a dictionary to excel


import pandas as pd

student = {"Name": "Vishvajit Rao", "age": 23, "Occupation": "Developer","Language": "Python"}

# convert into dataframe
df = pd.DataFrame(data=student, index=[1])

#convert into excel
df.to_excel("students.xlsx", index=False)
print("Dictionary converted into excel...")

After the above code is executed successfully, students.xlsx will create.

Convert List of Dictionary to Excel in Python

Most of the time you have a large list of dictionaries and you want to convert them into excel, then it is also possible, Let’s see how
can you do that.

Example: Convert list of dictionaries to excel


import pandas as pd

student = [{"Name": "Vishvajit Rao", "age": 23, "Occupation": "Developer","Skills": "Python"},
{"Name": "John", "age": 33, "Occupation": "Front End Developer","Skills": "Angular"},
{"Name": "Harshita", "age": 21, "Occupation": "Tester","Skills": "Selenium"},
{"Name": "Mohak", "age": 30, "Occupation": "Full Stack","Skills": "Python, React and MySQL"}]

# convert into dataframe
df = pd.DataFrame(data=student)

#convert into excel
df.to_excel("students.xlsx", index=False)
print("Dictionary converted into excel...")

Output

How to Convert Dictionary to Excel in Python

Conclusion

So we have seen all about how to convert dictionary to excel in Python with the help of examples. This is one of the most important especially when you are generating reports from large dictionaries into an excel file because dictionaries are not understandable by non-techie guys so you have an option to convert it into an excel file.

I hope this article will help you. if you like this article, please share and keep visiting for further interesting articles.

Previous Article
Next Article

Related Posts