Python Pandas : How to Create DataFrame from dictionary ?
Tags: python, programming
Published: 2021-06-28 06:26:42

<h2>How to create DataFrame from dictionary in Python ?</h2>
In this article we will discuss about various ways of creating DataFrame object from dictionaries.

So, let's start exploring different approaches to achieve this result.
<ul>
 	<li><a href="#Create_DataFrame_from_Dictionary_using_default_Constructor">Create DataFrame from Dictionary using default Constructor</a></li>
 	<li><a href="#Create_DataFrame_from_Dictionary_with_custom_indexes">Create DataFrame from Dictionary with custom indexes</a></li>
 	<li><a href="#Create_DataFrame_from_Dictionary_and_skip_data">Create DataFrame from Dictionary and skip data</a></li>
 	<li><a href="#Create_DataFrame_from_Dictionary_with_different_Orientation">Create DataFrame from Dictionary with different Orientation</a></li>
 	<li><a href="#Create_DataFrame_from_nested_Dictionary">Create DataFrame from nested Dictionary</a></li>
</ul>
Want to expert in the python programming language? Exploring <a href="https://btechgeeks.com/python-data-analysis-using-pandas/">Python Data Analysis using Pandas</a> tutorial changes your knowledge from basic to advance level in python concepts.
<h3><a id="Create_DataFrame_from_Dictionary_using_default_Constructor"></a>Method-1 : Create DataFrame from Dictionary using default Constructor :</h3>
In python DataFrame constructor accepts n-D array, dictionaries etc.
<pre><span class="enlighter-text">Syntax : pandas.</span><span class="enlighter-m1">DataFrame</span><span class="enlighter-g1">(</span><span class="enlighter-text">data=</span><span class="enlighter-e1">None</span><span class="enlighter-text">, index=</span><span class="enlighter-e1">None</span><span class="enlighter-text">, columns=</span><span class="enlighter-e1">None</span><span class="enlighter-text">, dtype=</span><span class="enlighter-e1">None</span><span class="enlighter-text">, copy=</span><span class="enlighter-e0">False</span><span class="enlighter-g1">)</span></pre>
<pre class="EnlighterJSRAW" data-enlighter-language="python">#program :

# import pandas library
import pandas as pd
  
# dictionary with list object in values
data = {
    'Name' : ['Satya', 'Omm', 'Rakesh'],
    'Age' : [21, 21, 23],
    'From' : ['BBSR', 'RKL', 'KDP']
}
  
# creating a Dataframe object 
df_obj = pd.DataFrame(data)
  
df_obj


</pre>
<pre>Output :

         Name    Age   From
0      Satya      21      BBSR
1      Omm      21      RKL
2      Rakesh    23       KDP</pre>
All keys in dictionary are converted to column name and values in dictionaries converted column data.
<h3><a id="Create_DataFrame_from_Dictionary_with_custom_indexes"></a>Method-2 : Create DataFrame from Dictionary with custom indexes :</h3>
<pre class="EnlighterJSRAW" data-enlighter-language="python">#program :

# import pandas library
import pandas as pd
  
# dictionary with list object in values
data = {
    'Name' : ['Satya', 'Omm', 'Rakesh'],
    'Age' : [21, 21, 23],
    'From' : ['BBSR', 'RKL', 'KDP']
}
  
# creating a Dataframe object 
df_obj = pd.DataFrame(data, index = ['a','b','c'])
  
df_obj

</pre>
<pre>Output :
        Name    Age     From
a      Satya      21      BBSR
b     Omm       21     RKL
c     Rakesh     23     KDP</pre>
By passing index list, we can avoid the default index.
<h3><a id="Create_DataFrame_from_Dictionary_and_skip_data"></a>Method-3 : Create DataFrame from Dictionary and skip data</h3>
By skipping some of the items of dictionary, we can also create a DataFrame object from dictionary

Let's see the implementation of that.
<pre class="EnlighterJSRAW" data-enlighter-language="python">#program :

# import pandas library
import pandas as pd
  
# dictionary with list object in values
data = {
    'Name' : ['Satya', 'Omm', 'Rakesh'],
    'Age' : [21, 21, 23],
    'From' : ['BBSR', 'RKL', 'KDP']
}
  
# creating a Dataframe object 
#items skipped with key 'Age'
df_obj = pd.DataFrame(data, columns=['name', 'From'])
  
df_obj


</pre>
<pre>Output :
       Name        From
0      Satya        BBSR
1     Omm          RKL
2     Rakesh       KDP</pre>
<h3><a id="Create_DataFrame_from_Dictionary_with_different_Orientation"></a>Method-4 : Create DataFrame from Dictionary with different Orientation</h3>
DataFrame can also be created from dictionary using <code>DataFrame.from_dict()</code> function.
<pre><span class="enlighter-text">DataFrame.</span><span class="enlighter-m1">from_dict</span><span class="enlighter-g1">(</span><span class="enlighter-text">data, orient=</span><span class="enlighter-s0">'columns'</span><span class="enlighter-text">, dtype=</span><span class="enlighter-e1">None</span><span class="enlighter-g1">)</span></pre>
It accepts orientation too, where we can pass the orientation as index then the keys which were used as columns during creating DataFrame now they will be used as index.

Let's see the implementation of that.
<pre class="EnlighterJSRAW" data-enlighter-language="python">#program :

# import pandas library
import pandas as pd
  
# dictionary with list object in values
data = {
    'Name' : ['Satya', 'Omm', 'Rakesh'],
    'Age' : [21, 21, 23],
    'From' : ['BBSR', 'RKL', 'KDP']
}
  
# creating a Dataframe object 
#items skipped with key 'Age'
df_obj = pd.DataFrame(data,orient='index')
  
df_obj


</pre>
<pre>Output :

<span class="enlighter-n1">             0</span>           <span class="enlighter-n1">1</span>            <span class="enlighter-n1">2</span>

<span class="enlighter-text">Aame   Satya   Omm     Rakesh</span>

<span class="enlighter-text">From     BBSR    RKL       KDP</span>

<span class="enlighter-text">Age       21         21         23</span></pre>
<h3><a id="Create_DataFrame_from_nested_Dictionary"></a>Method-5 : Create DataFrame from nested Dictionary :</h3>
Suppose we have a nested dictionary, then we ill directly pass it in DataFrame constructor where the keys of dictionary will be used as column.

Let's see the implementation of that.
<pre class="EnlighterJSRAW" data-enlighter-language="python">#program :

# import pandas library
import pandas as pd
  
# dictionary with list object in values
# Nested Dictionary
data = { 
0 : {
    'Name' : 'Satya',
    'Age' : 21,
    'From' : 'BBSR'
    },
1 : {
    'Name' : 'Omm',
    'Age' : 21,
    'From' : 'RKL'
    },
2 : {
    'Name' : 'Rakesh',
    'Age' : 23,
    'From' : 'KDP'
    }
}
  
# creating a Dataframe object 
#items skipped with key 'Age'
df_obj = pd.DataFrame(data)
  
df_obj

</pre>
<pre>Output :
<span class="enlighter-n1">             0</span>           <span class="enlighter-n1">1</span>            <span class="enlighter-n1">2</span>
<span class="enlighter-text">Aame   Satya   Omm     Rakesh</span>
<span class="enlighter-text">From     BBSR    RKL       KDP</span>
<span class="enlighter-text">Age       21         21         23</span></pre>
<strong>Read more Articles on Python Data Analysis Using Padas - Creating Dataframe Objects</strong>:
<ul>
 	<li><a href="https://btechgeeks.com/pandas-how-to-create-an-empty-dataframe-and-append-rows-columns-to-it-in-python/">How to create an empty DataFrame and add data to it later?</a></li>
 	<li><a href="https://btechgeeks.com/python-pandas-how-to-convert-lists-to-a-dataframe/">How to convert lists to a dataframe?</a></li>
 	<li><a href="https://btechgeeks.com/read-csv-file-to-dataframe-with-custom-delimiter-in-python/">How to read a csv file to Dataframe with custom delimiter?</a></li>
 	<li><a href="https://btechgeeks.com/pandas-skip-rows-while-reading-csv-file-to-a-dataframe-using-read_csv-in-python/">How to skip rows while reading csv file to a Dataframe using read_csv()?</a></li>
</ul>