Python Pandas

Pandas DataFrame from Dict

The Pandas DataFrames are two-dimensional data structures that can store various types of Python data. DataFrames are widely utilized for data science/data processing and machine learning tasks. There are multiple ways to create a Pandas DataFrame, one way is using the Python dictionary (collection of key-value pairs).

This tutorial will present you with a complete overview of the “DataFrame.from_dict()” method, using numerous examples. To get started let’s look at the below contents:

What is the “DataFrame.from_dict()” Method in Python?

In Python, the “DataFrame.from_dict()” method is a class method of the Pandas library. It is used to create/construct Pandas DataFrame from a dictionary of array-like or dictionary type.

Syntax

DataFrame.from_dict(data, orient='columns', dtype=None, columns=None)

 

Parameters

The above syntax states that:

  • The “data” parameter represents the array like a dictionary object.
  • The “orient” parameter specifies the data orientation.
  • The “dtype” parameter represents the data type to force.
  • The “columns” parameter specifies the column labels to use.

Return Value

The “DataFrame.from_dict()” method retrieves the DataFrame.

Example 1: Constructing DataFrame From Dictionary with Key Value as List

Here, the “pandas” module is imported, and the dictionary is defined with a key value as a list. Next, the “pandas.DataFrame.from_dict()” method is used to construct the DataFrame from the input dictionary:

import pandas
dict1 = {'name': ['Joseph', 'Jason', 'Lily', 'Anna'],'age': ['25', '18', '17', '16']}
print(dict1, '\n')
data = pandas.DataFrame.from_dict(dict1)
print(data)

 

The DataFrame with dictionary key value as columns labels and dictionary value as columns value has been created:

Example 2: Constructing DataFrame From List of Dictionaries

We can also construct the DataFrame from the list of dictionaries. In the code below, first, we defined the list of dictionaries. After that, it is passed to the “pandas.DataFrame.from_dict()” function and retrieve the DataFrame:

import pandas
data = [{'Name': 'Joseph', 'Age': 20, 'Height': 6.0},
        {'Name': 'Anna', 'Age': 17, 'Height': 5.5},
        {'Name': 'Lily', 'Age': 22, 'Height': 4.9 }]
print(pandas.DataFrame.from_dict(data))

 

The DataFrame has been created:

Example 3: Constructing DataFrames From Dictionary with Different Orientation

We can also use the “orient” parameter of the “pandas.DataFrame.from_dict()” method to construct the DataFrame in different orientations. For example, the “index” and “columns” parameters are passed to the “pandas.DataFrame.from_dict()” method. Then, retrieve the DataFrame in index and columns orientation:

import pandas
data = {'Name': ['Joseph', 'Anna', 'Lily'],
        'Age': [20, 17, 22],
        'Height': [6.0, 5.5, 4.9]}
print(pandas.DataFrame.from_dict(data, orient ='index'), '\n')
print(pandas.DataFrame.from_dict(data, orient ='columns'))

 

The below snippet shows the index orientation and column orientation DataFrame:

Alternative Method: Create DataFrame From Dictionary Using the “Pandas.DataFrame()”

We can also use the “pandas.DataFrame()” method to construct DataFrame from the particular dictionary. In the below-stated code, the dictionary with key value as a list is declared and passed to the “pandas.DataFrame()” function. Then, the function retrieves the Pandas DataFrame with a specified index by using the “index” parameter.

import pandas
data = {'Name': ['Joseph', 'Anna', 'Lily'],
        'Age': [20, 17, 22],
        'Height': [6.0, 5.5, 4.9]}
print(pandas.DataFrame(data, index = ['a', 'b', 'c']))

 

The DataFrame has been created from the dictionary:

Conclusion

The “DataFrame.from_dict()” method in Python accepts the dictionary object as an argument and yields the DataFrame. We can pass the single dictionary or list of dictionary or dictionary key values as a list to the DataFrame.from_dict() method and construct DataFrame. The “Pandas.DataFrame()” method can also be used to construct the Pandas DataFrame from the Dictionary with index. This write-up presented different ways to construct a DataFrame from a particular dictionary.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.