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

Here I am going to convert only a single Python Dictionary to an Excel file using the Python Pandas library.

In this example, we will see how to convert the 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 be created.

Convert List of Dictionary to Excel in Python

Sometimes you may want to convert the list of Python dictionaries to an Excel file. then again, you can follow the below approach.

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 a 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

Useful Pandas Articles


Conclusion

So we have seen all about how to convert a 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.

Thanks for your valuable time… 🙏🙏

How to Convert Excel to Dictionary in Python
How To Convert a Tuple To a List in Python?

Related Posts