How to Convert a List to a Pandas DataFrame in Python?

As a data scientist working with US census data, I recently faced the challenge of converting a large list of data into a structured DataFrame for analysis. In this tutorial, I will explain how to convert a list to a Pandas DataFrame in Python with suitable examples and screenshots of executed example code.

Convert a List to a Pandas DataFrame in Python

Let’s start with a basic example. Suppose you have a list of names of US states:

states = ['California', 'Texas', 'Florida', 'New York', 'Illinois']

To convert this list to a DataFrame, you can simply pass the list to the pd.DataFrame() constructor:

import pandas as pd

states_df = pd.DataFrame(states, columns=['State'])
print(states_df)

Output:

        State
0  California
1       Texas
2     Florida
3    New York
4    Illinois

You can see the output in the screenshot below.

Convert a List to a Pandas DataFrame in Python

Converting a list of US state names into a Pandas DataFrame is simple using the pd.DataFrame() constructor.

Read How to Filter Lists in Python?

Convert a List of Lists to a DataFrame

In many cases, you may have a list of lists in Python, where each inner list represents a row of data. For example, let’s consider a list of US city data:

cities = [
    ['New York', 'New York', 8336817],
    ['Los Angeles', 'California', 3979576],
    ['Chicago', 'Illinois', 2693976],
    ['Houston', 'Texas', 2320268],
    ['Phoenix', 'Arizona', 1680992]
]

To convert this list of lists to a DataFrame, you can pass the list directly to pd.DataFrame() and specify the column names:

cities_df = pd.DataFrame(cities, columns=['City', 'State', 'Population'])
print(cities_df)

Output:

           City        State  Population
0      New York    New York     8336817
1  Los Angeles   California     3979576
2       Chicago    Illinois     2693976
3       Houston       Texas     2320268
4       Phoenix     Arizona     1680992

You can see the output in the screenshot below.

How to Convert a List to a Pandas DataFrame in Python

Check out How to Get the Index of an Element in a List in Python?

Convert a List of Dictionaries to a DataFrame

Another common scenario is having a list of dictionaries in Python, where each dictionary represents a row of data with key-value pairs as column names and values. Let’s consider an example of US presidential data:

presidents = [
    {'Name': 'Joe Biden', 'Party': 'Democratic', 'Term Start': 2021},
    {'Name': 'Donald Trump', 'Party': 'Republican', 'Term Start': 2017},
    {'Name': 'Barack Obama', 'Party': 'Democratic', 'Term Start': 2009},
    {'Name': 'George W. Bush', 'Party': 'Republican', 'Term Start': 2001}
]

To convert this list of dictionaries to a DataFrame, you can pass the list to pd.DataFrame():

presidents_df = pd.DataFrame(presidents)
print(presidents_df)

Output:

             Name        Party  Term Start
0       Joe Biden   Democratic        2021
1    Donald Trump  Republican        2017
2    Barack Obama   Democratic        2009
3  George W. Bush  Republican        2001

You can see the output in the screenshot below.

Convert a List to a Pandas DataFrame in Python to dictionaries

Read How to Merge Lists Without Duplicates in Python?

Handle Missing Data

In real-world scenarios, you may encounter missing data in your lists. Pandas provides various options to handle missing data when converting a list to a DataFrame. Let’s consider an example of US state population data with missing values:

state_data = [
    ['California', 39512223],
    ['Texas', 28995881],
    ['Florida', 21477737],
    ['New York', 19453561],
    ['Illinois', None]
]

By default, Pandas will fill missing values with NaN (Not a Number) when converting the list to a DataFrame:

state_df = pd.DataFrame(state_data, columns=['State', 'Population'])
print(state_df)

Output:

        State  Population
0  California   39512223.0
1       Texas   28995881.0
2     Florida   21477737.0
3    New York   19453561.0
4    Illinois          NaN

You can choose to fill in missing values with a specific value using the fillna() method:

state_df['Population'] = state_df['Population'].fillna(0)
print(state_df)

Output:

        State  Population
0  California   39512223.0
1       Texas   28995881.0
2     Florida   21477737.0
3    New York   19453561.0
4    Illinois         0.0

Read How to Convert String to List in Python?

Conclusion

In this tutorial, I explained how to convert a list to a Pandas DataFrame in Python. I discussed converting a list of lists to a DataFrame, converting a list of dictionaries to a DataFrame, and handling missing data.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.