How to Convert a Dictionary to Pandas DataFrame?

In Python, the dictionary and Pandas DataFrame are both used to store collections of Data. The dictionary stores data collection as “Key-Value” pairs. While Pandas DataFrame is a combination of rows and columns, very similar to 2-D arrays, it stores and organizes data. In python, to represent the dictionary’s data in a more structured and organized way, the dictionary can be converted into Pandas DataFrame.

This blog post uses different methods to convert a Dictionary to Pandas DataFrame using numerous examples:

Method 1: Using the “pd.DataFrame.from_dict()”

The “pd.DataFrame.from_dict()” function accepts a dictionary as an argument and converts it into a Pandas DataFrame. Let’s understand it by the following examples:

Example 1: Convert a Dictionary to Pandas DataFrame

In the below code, a dictionary named “dict_data” is converted into DataFrame:

Code:

import pandas
dict_data = {'Name': ['Lily','Joseph', 'ALex'],'Age': ['22','24','26']}
dataframe_data = pandas.DataFrame.from_dict(dict_data)
print (dataframe_data, '\n')
print (type(dataframe_data))

In the above code:

  • The dictionary with multiple key values is initialized in the program.
  • The “pd.DataFrame.from_dict” accepts the dictionary as an argument and returns the corresponding DataFrame.

Output:

The above output shows that the input Dictionary has been converted into a pandas DataFrame.

Example 2: Convert a Dictionary to Pandas DataFrame With Key as an Index

In the code given below, the dictionary is converted to pandas DataFrame, and the specific column is set as an index of DataFrame.

Code: 

import pandas
dict_data = {'Name': ['Lily','Joseph', 'ALex'],'Age': ['22','24','26']}
dataframe_data = pandas.DataFrame.from_dict(dict_data, orient ='index')
print (dataframe_data, '\n')
print (type(dataframe_data))

In the above code:

  • The “pandas.DataFrame.from_dict()” accepts the dictionary and “orient = index” as arguments and returns the DataFrame with the key value as an index.

Output: 

The above snippet shows the given dictionary has been converted into a DataFrame. The dictionary keys are used as the index in the converted DataFrame.

Method 2: Using pd.DataFrame() Function 

The “pd.DataFrame” is used to create the Pandas DataFrame from the input dictionary. The following example code snippet demonstrates the working of the “pd.DataFrame()” function:

Code:

import pandas
dict_data = {'Name': ['Lily','Joseph', 'ALex'],'Age': ['22','24','26']}
dataframe_data = pandas.DataFrame(dict_data)
print (dataframe_data, '\n')
print (type(dataframe_data))

In the above code:

  • The “pd.DataFrame()” accepts a dictionary named “dict_data” and retrieves the pandas DataFrame.

Output: 

The dictionary has been converted into Pandas DataFrame using the “pd.DataFrame()” function.

Method 3: Using the “dict.items()” Along With “pd.DataFrame()” Function

The “dict.items()” function retrieves the dictionary keys and their value in the form of a list. This function is used along with “pd.DataFrame()” to convert a dictionary into pandas DataFrame: For further understanding, let’s have a look at the below code:

Code:

import pandas
dict_data = {'Lily': '22','Joseph': '22','Alex': '26'}
data = list(dict_data.items())
dataframe_data = pandas.DataFrame(data,columns = ['Name','Age'])
print (dataframe_data, '\n')
print (type(dataframe_data))

In the above code:

  • The “dict.items()” is used to get the key and its value from the dictionary in the form of a list of tuples.
  • The list() function is used to convert the list of tuples into a separate list and pass the value as an argument to the “pd.DataFrame()” function.
  • The “pd.DataFrame()” function accepts the list value of “dict.items()” and converts it into pandas DataFrame.

Output:

The dictionary has been converted into Pandas DataFrame using the “pd.DataFrame()” and “dict.items()” functions.

Conclusion

To convert a Dictionary to Pandas DataFrame, the “pd.DataFrame.from_dict()”, “pd.DataFrame()”, and “dict.items()” along with “pd.DataFrame()” functions are used. The “DataFrame.from_dict()” function is used to convert the Dictionary into DataFrame with the original index and specific name as an index. The “dict.items()” is also used along with the “pd.DataFrame()” to convert the dictionary into DataFrame. This post presented a detailed guide on converting a dictionary to a pandas data frame using suitable examples.