Signup/Sign In
PUBLISHED ON: MARCH 17, 2021

Pandas DataFrame from_dict() Method

In this tutorial, we will learn the Python pandas DataFrame.from_dict() method. It constructs DataFrame from dictionary of array-like or dicts type. It creates a DataFrame object from the dictionary by columns or by index allowing dtype specification.

The below shows the syntax of the DataFrame.from_dict() method.

Syntax

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

Parameters

data: dict. Of the form {field : array-like} or {field : dict}.

orient:{‘columns’, ‘index’}, default ‘columns’. The “orientation” of the data. If the keys of the passed dict should be the columns of the resulting DataFrame, pass ‘columns’ (default). Otherwise, if the keys should be rows, pass ‘index’.

dtype: dtype, default None. Data type to force, otherwise infer.

columns: list, default None. Column labels to use when orient='index'. Raises a ValueError if used with orient='columns'.

Example: Create DataFrame from the dictionary

When creating the DataFrame from the dictionary by default, the keys of the dict become the DataFrame columns.

#import pandas as pd
import pandas as pd
#creating dictionary
data = {'key_1': [3, 2, 1, 0], 'key_2': ['a', 'b', 'c', 'd']}
print("The python dictionary is:",data)
#creating DataFrame
df=pd.DataFrame.from_dict(data)
print("----------The DataFrame is----------")
print(df)

Once we run the program we will get the following output.


The python dictionary is: {'key_1': [3, 2, 1, 0], 'key_2': ['a', 'b', 'c', 'd']}
----------The DataFrame is----------
key_1 key_2
0 3 a
1 2 b
2 1 c
3 0 d

Example: Create DataFrame from the dictionary

When orient='index' in the DataFrame.The keys of the dict become the DataFrame rows. The below example shows the same.

#import pandas as pd
import pandas as pd
#creating dictionary
data = {'key_1': [3, 2, 1, 0], 'key_2': ['a', 'b', 'c', 'd']}
print("The python dictionary is:",data)
#creating DataFrame
df=pd.DataFrame.from_dict(data,orient='index')
print("----------The DataFrame is----------")
print(df)

Once we run the program we will get the following output.


The python dictionary is: {'key_1': [3, 2, 1, 0], 'key_2': ['a', 'b', 'c', 'd']}
----------The DataFrame is----------
0 1 2 3
key_1 3 2 1 0
key_2 a b c d

Example: Create DataFrame from the dictionary

When using the ‘index’ orientation, the column names can be specified manually. See the below example.

#import pandas as pd
import pandas as pd
#creating dictionary
data = {'key_1': [3, 2, 1, 0], 'key_2': ['a', 'b', 'c', 'd']}
print("The python dictionary is:",data)
#creating DataFrame
df=pd.DataFrame.from_dict(data,orient='index',columns=['A', 'B', 'C', 'D'])
print("----------The DataFrame is----------")
print(df)

Once we run the program we will get the following output.


The python dictionary is: {'key_1': [3, 2, 1, 0], 'key_2': ['a', 'b', 'c', 'd']}
----------The DataFrame is----------
A B C D
key_1 3 2 1 0
key_2 a b c d

Conclusion

In this tutorial, we learned the Python pandas DataFrame.from_dict()method. We learned the syntax, parameters and by applying this method on the DataFrame to understand the DataFrame.from_dict() method.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.