How to create a pandas dataframe from a python dictionary ?

Published: November 27, 2022

Tags: Python; Pandas; Dataframe; Dictionary;

DMCA.com Protection Status

Examples of how to create a pandas dataframe from a python dictionary:

Create a dataframe from a python dictionary (method 1)

To create a dataframe from a python dictionary:

d = {
    'Name': ['Ben', 'John', 'Emma', 'Zoe'],
    'Age': [40, 56, 34, 12]
}

a solution is to do

import pandas as pd

df = pd.DataFrame(d)

gives

   Name  Age
0   Ben   40
1  John   56
2  Emma   34
3   Zoe   12

Create a dataframe from a python dictionary (method 2)

Another solution is to use pandas.DataFrame.from_dict

df = pd.DataFrame.from_dict(d)

print(df)

gives

   Name  Age
0   Ben   40
1  John   56
2  Emma   34
3   Zoe   12

The advantage is to use parameters such as orient:

df = pd.DataFrame.from_dict(d, orient='index')

gives then

        0     1     2    3
Name  Ben  John  Emma  Zoe
Age    40    56    34   12

Add column names:

df = pd.DataFrame.from_dict(d, orient='index', columns=['User 1', 'User 2', 'User 3', 'User 4'])

gives

     User 1 User 2 User 3 User 4
Name    Ben   John   Emma    Zoe
Age      40     56     34     12

Add a new row into a dataframe

Another example, add a new row into an existing dataframe:

import pandas as pd

d = {
    'Name': ['Ben', 'John', 'Emma', 'Zoe'],
    'Age': [40, 56, 34, 12]
}

df = pd.DataFrame.from_dict(d)

    Name  Age
0   Ben   40
1  John   56
2  Emma   34
3   Zoe   12

Create a new dataframe from a python dictionary

new_d = {
    'Name': ['Paula'],
    'Age': [67]
}

df_new_row = pd.DataFrame.from_dict(new_d)

To add a new row, a solution is to use concat() (see How to merge (concatenate) two or more dataframe columns into one column with pandas ? and How to add a new row at the end of a pandas DataFrame in pandas ?)

df = pd.concat([df,df_new_row], ignore_index=True)

gives

     Name  Age
0    Ben   40
1   John   56
2   Emma   34
3    Zoe   12
4  Paula   67

Fix error "If using all scalar values, you must pass an index"

new_d = {
    'Name': 'Paula',
    'Age': 67
}

df_new_row = pd.DataFrame.from_dict(new_d)

returns

 ValueError: If using all scalar values, you must pass an index

This is because values of the dictionary new_d are not a list.

To fix that just add []:

new_d = {
    'Name': ['Paula'],
    'Age': [67]
}

df_new_row = pd.DataFrame.from_dict(new_d)