Python Pandas : How to Create DataFrame from dictionary ?

How to create DataFrame from dictionary in Python ?

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.

Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.

Method-1 : Create DataFrame from Dictionary using default Constructor :

In python DataFrame constructor accepts n-D array, dictionaries etc.

Syntax : pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)
#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


Output :

         Name    Age   From
0      Satya      21      BBSR
1      Omm      21      RKL
2      Rakesh    23       KDP

Method-2 : Create DataFrame from Dictionary with custom indexes :

#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

Output :
        Name    Age     From
a      Satya      21      BBSR
b     Omm       21     RKL
c     Rakesh     23     KDP

Method-3 : Create DataFrame from Dictionary and skip data

#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


Output :
       Name        From
0      Satya        BBSR
1     Omm          RKL
2     Rakesh       KDP

Method-4 : Create DataFrame from Dictionary with different Orientation

DataFrame.from_dict()
DataFrame.from_dict(data, orient='columns', dtype=None)
#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


Output :

             0           1            2

Aame   Satya   Omm     Rakesh

From     BBSR    RKL       KDP

Age       21         21         23

Method-5 : Create DataFrame from nested Dictionary :

#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

Output :
             0           1            2
Aame   Satya   Omm     Rakesh
From     BBSR    RKL       KDP
Age       21         21         23

Read more Articles on Python Data Analysis Using Padas - Creating Dataframe Objects:

16