In this article we will discuss different techniques to create a DataFrame object from dictionary.

Create DataFrame from Dictionary using default Constructor

DataFrame constructor accepts a data object that can be ndarray, dictionary etc. i.e.

But if we are passing a dictionary in data, then it should  contain a list like objects in value field like Series, arrays or lists etc i.e.

On Initialising a DataFrame object with this kind of dictionary, each item (Key / Value pair) in dictionary will be converted to one column i.e. key will become Column Name and list in the value field will be the column data i.e.

It will create a DataFrame object like this,

All the keys in dictionary will be converted to column names and lists in each its value field will we converted to column Data.

Create DataFrame from Dictionary with custom indexes

We can also pass the index list to the DataFrame constructor to replace the default index list i.e.

It will create a DataFrame object like this,

Create DataFrame from not compatible dictionary

As DataFrame constructor accepts a dictionary which should contain a list like objects in values. But what if we have a dictionary that doesn’t have lists in value i.e.

If we will directly pass this dictionary to DataFrame constructor then it will throw following error,
ValueError: If using all scalar values, you must pass an index

So, how to create a two column DataFrame object from this kind of dictionary and put all keys and values as these separate columns like this,

For that we will create a list to tuples (key / value) from this dictionary and pass it to another dataframe constructor that accepts a list i.e.

It will create a DataFrame object like this,

Create DataFrame from Dictionary and skip data

But we want to create a DataFrame object from dictionary by skipping some of the items. Let’s see how to do that,

Suppose we have dictionary like this,

Create a DataFrame from this by skipping items with key ‘age’ ,

As in columns parameter we provided a list with only two column names. So, DataFrame should contain only 2 columns i.e.

Create DataFrame from Dictionary with different Orientation

We can create a DataFrame from dictionary using DataFrame.from_dict() function too i.e.

It accepts a dictionary and orientation too. By default orientation is columns it means keys in dictionary will be used as columns while creating DataFrame.
We can also pass the orientation as ‘index’, which changes the default orientation and makes the keys in dictionary as index i.e.

Dictionary :

Create DataFrame with index in orientation i.e.

It will create a DataFrame object like this,

Create DataFrame from nested Dictionary

Suppose we have a nested dictionary i.e.

It has 3 items in it and each item contains a dictionary in value field which internally contains the same keys but with different value.

We can directly pass it in DataFrame constructor, but it will use the keys of dict as columns and  DataFrame object like this will be generated i.e.

It will create a DataFrame object like this,

Now let’s transpose this matrix to swap the column with indexes i.e. data will be more readable with this i.e.

Now contents of DataFrame will be like this,

 

Complete example is as follows,

Output:

 

Click Here to Subscribe for more Articles / Tutorials like this.