Pandas: How to create a DataFrame from a dictionary of lists

Updated: February 19, 2024 By: Guest Contributor Post a comment


Pandas is a powerful Python library that provides numerous tools for data analysis and manipulation. One of the core components of Pandas is the DataFrame, which can be thought of as a relational data table, similar to a spreadsheet. In this tutorial, we will explore how to create a DataFrame from a dictionary of lists, a common data structure in Python that allows for the straightforward creation of structured data.

We’ll start with the basics and gradually move on to more advanced examples, ensuring that you have a solid understanding of how this process works and how it can be customized to meet your data handling needs.

Getting Started with Pandas

Before diving into the specifics, let’s ensure you have Pandas installed in your Python environment. You can install Pandas using pip:

pip install pandas

Once Pandas is installed, you can import it in your Python script like this:

import pandas as pd

Creating a Simple DataFrame from a Dictionary of Lists

Let’s start with the most basic example. Assume we have data about students, including their names, ages, and grades. This data is stored in a dictionary where keys are column names, and values are lists containing the data for each column.

data = {
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 26, 24],
    'Grade': ['A', 'B', 'C']

# Create DataFrame
df = pd.DataFrame(data)

# Display the DataFrame

This code snippet creates a DataFrame and displays the first few records. The output should look like this:

      Name  Age Grade
0    Alice   25     A
1      Bob   26     B
2  Charlie   24     C

Specifying Column Order

By default, Pandas orders columns alphabetically when creating a DataFrame from a dictionary. However, you can specify the order manually using the columns parameter to ensure your DataFrame matches your desired structure.

df = pd.DataFrame(data, columns=['Grade', 'Name', 'Age'])

# Display with specified column order

The output will now reflect the specified order:

  Grade     Name  Age
0     A    Alice   25
1     B      Bob   26
2     C  Charlie   24

Adding Index to DataFrame

In some cases, you might want to give your DataFrame rows custom index values instead of the default numerical index provided by Pandas. This can be done using the index parameter.

index_names = ['Student1', 'Student2', 'Student3']

# Create DataFrame with custom index
df = pd.DataFrame(data, index=index_names)

# Display the DataFrame

The DataFrame will now have custom index labels:

         Name  Age Grade
Student1 Alice   25     A
Student2   Bob   26     B
Student3 Charlie   24     C

Advanced Example: Combining Multiple Data Sources

As you become more comfortable with creating DataFrames from dictionaries of lists, you may encounter situations where your data comes from multiple sources and needs to be combined. Let’s look at how you can merge data from different dictionaries into a single DataFrame.

additional_data = {
    'Attendance': [True, False, True],
    'Sport': ['Football', 'Basketball', 'Swimming']

# Merge dictionaries
combined_data = {**data, **additional_data}

# Create DataFrame
df = pd.DataFrame(combined_data)

# Display the combined DataFrame

This code merges two dictionaries before creating the DataFrame, resulting in a more complex structure that includes additional columns:

      Name  Age Grade  Attendance       Sport
0    Alice   25     A        True    Football
1      Bob   26     B       False  Basketball
2  Charlie   24     C        True    Swimming

Handling Missing Data

In real-world scenarios, it’s common for your dictionary lists to have different lengths, resulting in missing data when converging into a DataFrame. Pandas automatically handles this by filling in missing values with NaN (Not a Number). Let’s simulate this scenario:

data_with_missing = {
    'Name': ['Alice', 'Bob', 'Charlie', 'Diana'],
    'Age': [25, 26, 24],
    'Grade': ['A', 'B', 'C', 'F']

# Create DataFrame with missing data
df = pd.DataFrame(data_with_missing)

# Display the DataFrame

The DataFrame will display NaN for missing values in the ‘Age’ column for ‘Diana’:

      Name   Age Grade
0    Alice  25.0     A
1      Bob  26.0     B
2  Charlie  24.0     C
3    Diana   NaN     F


Creating a DataFrame from a dictionary of lists is a fundamental task in data manipulation with Pandas. This tutorial covered the basic through advanced techniques, providing you with the knowledge to efficiently organize your data into structured form. Applying these methods will enable you to harness the full power of Pandas for your data analysis and manipulation tasks.