How to use from_dict to convert a Python dictionary to a Pandas dataframe?

There are many instances in which you’ll need to quickly create a dataframe, like we typically do in most of this website Python tutorials.

In today’s tutorial we’ll learn to user the pd.DataFrame.from_dict method that offers some useful options for converting dictionaries of lists to simple dataframes. In one of our upcoming tutorials will look into a more interesting technique for converting a nested dictionary (or list of dictionaries) to a Pandas df.

We’ll start by importing the Pandas package library into your local namespace in Python.

import pandas as pd

Now we’ll define the following dictionary. As you can see the dictionary consistst of key/value pairs. The values in this case are actually lists of elements.

sales_data = ({"Year": [ 2020, 2020, 2020] ,
         "Area": ["East", "West", "Midwest"], 
         "Revenue": [80000, 95000, 56500]})

Construct a dataframe from a dictionary

my_df = pd.DataFrame.from_dict(data=sales_data)
my_df.head()
YearAreaRevenue
02020East80000
12020West95000
22020Midwest56500

The dictionary keys are by default automatically set as the dataframe columns headers, whereas the value lists are converted to the dataframe columns. The rows are auto-assigned an index.

Create dataframe with index

Let’s look at a bit more complex case:

# orient and custom columns
my_df = pd.DataFrame.from_dict(data=sales_data, orient="index", columns =  ["COL_A", "COL_B", "COL_C"])

The parameter orient=index allows to indicate that the dictionary keys will represent the dataframe rows as shown below.

The columns parameter, allows to specify custom column names.

COL_ACOL_BCOL_C
Year202020202020
AreaEastWestMidwest
Revenue800009500056500

Let’s look into the dataframe index:

my_df.index()

The result will be:

Index(['Year', 'Area', 'Revenue'], dtype='object')

Set custom data types in your dataframe

What if you would like to set your dataframe column object types on creation, instead of accepting the ones arbitrarily assigned by Pandas when converting your dict object?

Here you go:

# using custom dtypes
my_df = pd.DataFrame.from_dict(data=sales_data, orient="columns").astype({"Year":int,"Area":object, "Revenue":float})
my_df.dtypes
Year        int32
Area       object
Revenue     int64
dtype: object

As you can see, the astype method allows us to define a dictionary that matches a Pandas datatype (int, int64, float, float64, datetime, objects etc’) to each of our dataframe columns. Note that you are able to specify dtypes only for part of your columns. In that case Pandas will assign the remaining data types automatically.

Leave a Comment