Create Pandas DataFrame from Dictionary


Creating dataframe from the dictionary is a fundamental and versatile operation in Pandas. Dictionaries provide a flexible way to organize data, and Pandas seamlessly convert them into tabular structures for efficient data analysis.

In this article, we will look at multiple different types of dictionaries and how to create DataFrame from them.

    Table of Contents

  1. DataFrame from Simple Dictionary
  2. DataFrame from Dictionary of Lists
  3. DataFrame from Dictionary of Dictionaries
  4. DataFrame from Dictionary of Mixed Types
  5. DataFrame from Dictionary of Datetime Values
  6. DataFrame from Dictionary with User Defined Index
  7. DataFrame from Dictionary with Required Columns
  8. DataFrame from Dictionary with Index and Column Swapped
  9. Conclusion

1. DataFrame from Simple Dictionary

Let's start with the simplest example of creating a dataframe from a dictionary. The following example creates a dataframe from a simple dictionary with key-value pairs.

Convert your simple dictionary into a list of tuples and pass it to the pd.DataFrame() function because the pd.DataFrame() function expects an iterable object.

import pandas as pd

# simple dictionary
data = {
    "HTML": "Web Development",
    "Java": "Application Development",
    "Python": "Data Science"
}

# πŸ‘‡ create dataframe from a list of tuples
df = pd.DataFrame(list(data.items()))

print(df)

Output:

        0                        1
0    HTML          Web Development
1    Java  Application Development
2  Python             Data Science

2. DataFrame from Dictionary of Lists

Most of the time, you will be working with a dictionary of lists. In this example, we will create a dataframe from a dictionary of lists.

Just pass your dictionary to the pd.DataFrame() function and it will automatically convert it into a dataframe.

import pandas as pd

# dictionary of lists
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35]}

# πŸ‘‡ create dataframe from dictionary
df = pd.DataFrame(data)

print(df)

Output:

      Name  Age
0    Alice   25
1      Bob   30
2  Charlie   35

3. DataFrame from Dictionary of Dictionaries

When a dictionary of dictionaries is used to create the DataFrame then the inner dictionary is converted into a column so make sure your internal dictionary consistently has the same keys.

import pandas as pd

# dictionary of dictionaries
data = {'Name': {'0': 'Alice', '1': 'Bob', '2': 'Charlie'},
        'Age': {'0': 25, '1': 30, '2': 35}}

# πŸ‘‡ create dataframe from dictionary
df = pd.DataFrame(data)

print(df)

Output:

      Name  Age
0    Alice   25
1      Bob   30
2  Charlie   35

4. DataFrame from Dictionary of Mixed Types

Pandas DataFrames can effortlessly handle dictionaries with mixed data types, converting them into a homogeneous format.

import pandas as pd

# Dictionary with Mixed Data Types
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 'Unknown', 35],
        'City': ['New York', 'San Francisco', 'Los Angeles']}

# πŸ‘‡ create dataframe from dictionary
df = pd.DataFrame(data)

print(df)

Output:

      Name      Age           City
0    Alice       25       New York
1      Bob  Unknown  San Francisco
2  Charlie       35    Los Angeles

5. DataFrame from Dictionary of Datetime Values

When dealing with temporal data, a dictionary with DateTime values can be directly transformed into a Pandas DataFrame.

This results in a DataFrame with a datetime column, facilitating time-series analysis.

import pandas as pd
from datetime import datetime

# Dictionary with Datetime Values
data = {'Date': [datetime(2022, 1, 1), datetime(2022, 1, 2), datetime(2022, 1, 3)],
        'Value': [10, 20, 15]}

# πŸ‘‡ create dataframe from dictionary of datetime values
df = pd.DataFrame(data)

print(df)

Output:

        Date  Value
0 2022-01-01     10
1 2022-01-02     20
2 2022-01-03     15

6. DataFrame from Dictionary with User Defined Index

By default the index of the dataframe is set to 0, 1, 2, 3, ... but you can customize it by passing a list of index values to the index parameter of the pd.DataFrame() function.

import pandas as pd

# dictionary of lists
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35]}

# πŸ‘‡ create dataframe from dictionary with user defined index
df = pd.DataFrame(data, index=['a', 'b', 'c'])

print(df)

Output:

      Name  Age
a    Alice   25
b      Bob   30
c  Charlie   35

7. DataFrame from Dictionary with Required Columns

Suppose you have a dictionary with 10 key-value pairs but you only want to create a dataframe with 3 columns, then you can pass the required columns as a list to the columns parameter of the pd.DataFrame() function and it will generate a dataframe with only those columns.

import pandas as pd

# dictionary of lists
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35],
        'City': ['New York', 'San Francisco', 'Los Angeles'],
        'Salary': [3456, 8765, 1265, 9463]}

# πŸ‘‡ create dataframe from dictionary with required columns
df = pd.DataFrame(data, columns=['Name', 'City'])

print(df)

Output:

      Name           City
0    Alice       New York
1      Bob  San Francisco
2  Charlie    Los Angeles

8. DataFrame from Dictionary with Index and Column Swapped

The following example changes column labels to index and index as column values while creating dataframe of dictionary.

import pandas as pd

# dictionary of lists
data = {'Name': ['Alice', 'Bob', 'Charlie'],
        'Age': [25, 30, 35]}

# πŸ‘‡ create dataframe from dictionary with index and column swapped
df = pd.DataFrame(data).T

print(df)

Output:

          0    1        2
Name  Alice  Bob  Charlie
Age      25   30       35

Conclusion

Creating Pandas DataFrames from dictionaries is a fundamental skill in the toolkit of any data analyst or scientist.

Now you can create dataframe from any type of dictionary with ease.

Happy Learning πŸ˜„