Pandas DataFrame From Dict – pd.df.from_dict()

You can create a DataFrame many different ways. One popular way to do it is creating a pandas DataFrame from dict, or dictionary.

There are two main ways to create a go from dictionary to DataFrame, using orient=columns or orient=index. Orient is short for orientation, or, a way to specify how your data is laid out.

  • Method 1 – Orient (default): columns = If you want the keys of your dictionary to be the DataFrame column names
  • Method 2 – Orient: index = If the keys of your dictionary should be the index values. You’ll need to be explicit about column names.

Pandas DataFrame From Dict

Orient = Columns

You use orient=columns when you want to create a Dataframe from a dictionary who’s keys you want to be the columns. Usually your dictionary values will be a list containing an entry for every row you have.

Check out the picture below to see

Pandas DataFrame From Dict - Create a DataFrame from a python dictionary. Orient is columns = Your column names will be your dictionary keys

Orient = Index

You use orient=Index when you want the values of your dictionary to be the index of your DataFrame. Be sure to specify your columns when creating your DataFrame or else they’ll just be numbers.

Check out the code sample below for an example using both methods to create a pandas dataframe from dict

In [1]:
import pandas as pd

Start with a dictionary of data

Creating a dataframe from a dictionary is easy and flexible. Let's look at two ways to do it here:

  1. Method 1 - Orient (default): columns = If you want the keys of your dictionary to be the DataFrame column names
  2. Method 2 - Orient: index = If the keys of your dictionary should be the index values. You'll need to be explicit about column names.

Method 1 - Orient (default): columns = If you want the keys of your dictionary to be the DataFrame column names

First create a dictionary of data where the keys are your column names. Here my column names will be 1) Name 2) Type 3) AvgBill

In [2]:
dict_data_column_keys = {
     "Name": ["Liho Liho", "500 Club", "Foreign Cinema", "The Square"],
     "Type": ["Restaurant", "Bar", "Restaurant", "Bar"],
     "AvgBill": [200.45, 34.64, 180.45, 45.54] 
    }

Then call pd.DataFrame.from_dict() and pass your data in the function. Because orient=columns is the default, we do not need to specify orient below.

In [3]:
pd.DataFrame.from_dict(dict_data_column_keys)
Out[3]:
NameTypeAvgBill
0Liho LihoRestaurant200.45
1500 ClubBar34.64
2Foreign CinemaRestaurant180.45
3The SquareBar45.54

Passing orient="columns" yields the same result since this is the default value

In [4]:
pd.DataFrame.from_dict(dict_data_column_keys, orient='columns')
Out[4]:
NameTypeAvgBill
0Liho LihoRestaurant200.45
1500 ClubBar34.64
2Foreign CinemaRestaurant180.45
3The SquareBar45.54

Method 2 - Orient: Rows = If the keys of your dataframe should be the DataFrame index

Let's again first create a dictionary, but this time the keys are your index. Here my index will be my previous "name" column and the columns will be 1) Type 2) AvgBill

In [5]:
dict_data_index_keys = {
     "Liho Liho": ["Restaurant", 200.45],
     "500 Club": ["Bar", 34.64],
     "Foreign Cinema": ["Restaurant", 180.45],
     "The Square": ["Bar", 45.54]
    }
In [6]:
pd.DataFrame.from_dict(dict_data_index_keys, orient='index')
Out[6]:
01
Liho LihoRestaurant200.45
500 ClubBar34.64
Foreign CinemaRestaurant180.45
The SquareBar45.54

Whoops, as you can see, I don't have any column names! Make sure to specify your column names if you do orient=index

In [7]:
pd.DataFrame.from_dict(dict_data_index_keys, orient='index', columns=['Type', 'AvgBill'])
Out[7]:
TypeAvgBill
Liho LihoRestaurant200.45
500 ClubBar34.64
Foreign CinemaRestaurant180.45
The SquareBar45.54

Reach out if you have any questions about going from a a dict to pandas DataFrame

Link to code above

Check out more Pandas functions on our Pandas Page

Official Documentation