Pandas dictionary to a dataframe

Create a Pandas DataFrame from Dictionary

Pandas dataframes are quite powerful for dealing with two-dimensional data in python. There are a number of ways to create a pandas dataframe, one of which is to use data from a dictionary. In this tutorial, we’ll look at how to create a pandas dataframe from a dictionary with some examples.

The pandas.DataFrame.from_dict() function is used to create a dataframe from a dict object. The dictionary should be of the form {field: array-like} or {field: dict}. The following is its syntax:

df = pandas.DataFrame.from_dict(data)

By default, it creates a dataframe with the keys of the dictionary as column names and their respective array-like values as the column values. If you want the dictionary keys to be row indexes instead, pass 'index' to the orient parameter (which is 'columns' by default).

Let’s look at some of the examples to better understand its usage –

import pandas as pd

# dictionary storing the data
data = {
    "Name": ["Jim", "Dwight", "Angela", "Tobi"],
    "Age": [26, 28, 27, 32],
    "Department": ["Sales", "Sales", "Accounting", "Human Resources"]
}

# dataframe from dict
df = pd.DataFrame.from_dict(data)

# print the dataframe
print(df)

Output:

     Name  Age       Department
0     Jim   26            Sales
1  Dwight   28            Sales
2  Angela   27       Accounting
3    Tobi   32  Human Resources

In the above example, the dataframe df is constructed from the dictionary data. And by default, the keys of the dict are treated as column names and their values as respective column values by the pandas dataframe from_dict() function.

The parameter orient tells the function about the orientation of the data. It takes 'columns' or 'index' and is 'columns' by default. If the keys in your dictionary represent row indexes then pass orient='index'

import pandas as pd

# dictionary storing the data
data = {
    "Jim": [26, "Sales"],
    "Dwight": [28, "Sales"],
    "Angela": [27, "Accounting"],
    "Tobi": [32, "Human Resources"]
}

# dataframe from dict
df = pd.DataFrame.from_dict(data, orient='index')

# print the dataframe
print(df)

Output:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

         0                1
Jim     26            Sales
Dwight  28            Sales
Angela  27       Accounting
Tobi    32  Human Resources

The created dataframe has keys as row indexes.

You can also pass the column names as a list to the columns parameter when creating a dataframe with orient='index'

import pandas as pd

# dictionary storing the data
data = {
    "Jim": [26, "Sales"],
    "Dwight": [28, "Sales"],
    "Angela": [27, "Accounting"],
    "Tobi": [32, "Human Resources"]
}

# dataframe from dict
df = pd.DataFrame.from_dict(data, orient='index', columns=['Age', 'Department'])

# print the dataframe
print(df)

Output:

        Age       Department
Jim      26            Sales
Dwight   28            Sales
Angela   27       Accounting
Tobi     32  Human Resources

In the above example, you can see that we have 'Age' and 'Department' as columns names instead of the default labels.

For more on the pandas.DataFrame.from_dict() function refer to its official documentation.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top