Are you looking for Python solutions to convert a Pandas DataFrame to a dictionary?
In Pandas Python, a DataFrame is a two-dimensional tabular data structure with labelled axes (Columns and rows), much like a spreadsheet. The essential three components of a DataFrame are rows and columns. Furthermore, DataFrames are one of the most common data structures used for data analysis due to their flexible and intuitive nature.
We can convert any other collection in Python, like a list, dictionary, array, etc., into a DataFrame. This article focuses on converting a dictionary into a DataFrame, so let’s begin.
As we all know, there are multiple ways of doing the same task in computer programming. Similarly, we use different methods to convert a dictionary into a DataFrame in this case.
The following are two of those solutions;
- Use dict.items() to convert a dictionary into a DataFrame
- Use from_dict() to convert a dictionary into a DataFrame
But before going into further details of DataFrames, first, let’s learn about dictionaries in Python.
Table of Contents
How to Create a Dictionary in Python?
A dictionary is a data structure that belongs to the collections class in Python, and it stores the data in key-value pairs. Each key has a corresponding value; to access the values, you have to use the specific key.
Let’s create a dictionary in Python.
Code
# create a dictionary student = { "Name": "Tom", "Age" : 26, "Program": "Software Engineering", "CGPA": 3.7 } print( student )
Output
{'Name': 'Tom', 'Age': 26, 'Program': 'Software Engineering', 'CGPA': 3.7}
Wow! This was super easy. 😃
Now that we have understood how to create a dictionary in Python, we can talk about converting the dictionary to a DataFrame in Python.
2 Methods to Convert a Dictionary to a DataFrame in Python
We can convert any collection, like an array, list, or dictionary, into a DataFrame using Python’s valuable and built-in functions. There could be other different ways of converting a dictionary into a DataFrame, but in this article, we’ll focus on the two built-in functions dict.items() and from_dict().
Method 1: Use from_dict() to Convert the Dictionary to DataFrame in Python
We can use the from_dict() function to convert a dictionary into a DataFrame in Python; let’s see a practical example.
Code
import pandas as pd # create a dictionary student = { "Name": "Tom", "Age" : 26, "Program": "Software Engineering", "CGPA": 3.7 } # convert the dictionary' student' into a dataframe 'df' df = pd.DataFrame.from_dict(student, orient ='index') print(df)
Output
Name Tom Age 26 Program Software Engineering CGPA 3.7
This statement df = pd.DataFrame.from_dict(student, orient =’index’) is responsible for the conversion of a dictionary into a DataFrame.
Method 2: Use dict.items() to Convert the Dictionary to DataFrame in Python
In Python, the dict.items()) is also used to convert a dictionary into a DataFrame in Python. It is a built-in method and works similarly to from_dict().
Code
import pandas as pd # create a dictionary student = { "Name": "Tom", "Age" : 26, "Program": "Software Engineering", "CGPA": 3.7 } # convert the dictionary' student' into a dataframe 'df' df = pd.DataFrame(list(student.items())) print(df)
Output
0 1 0 Name Tom 1 Age 26 2 Program Software Engineering 3 CGPA 3.7
The above 👆 are two examples; both work similarly and help us convert a dictionary into a DataFrame. Other than that, you can define series and combine them as a dictionary using the different built-in functions like join(), merge(), concat(), and append(). But that would be a little manual and lengthy process though it is still helpful in some scenarios.
Conclusion
To summarize this article on converting a dictionary into a pandas DataFrame with Python, we discussed how to create a dictionary as well as the different methods used to convert a dictionary into a DataFrame.
Let’s have a quick recap of the topics discussed in this article
- How to create a dictionary in Python?
- How to convert a dictionary into a DataFrame in Python?
- Use the from_dict() function to convert a dictionary into a DataFrame.
- Use the dict.items() function to convert a dictionary into a DataFrame.
Time to explore more! Name any other function that converts a dictionary into a DataFrame.