Convert A Pandas DataFrame To A List In Python

As a Python developer working extensively with data, I often find myself needing to convert Pandas DataFrames to lists. This conversion is a common requirement when you need to pass data to functions that expect list inputs or when working with JavaScript libraries that require list-formatted data.

In this tutorial, I’ll walk you through different methods to convert a DataFrame to a list in Python, explaining each approach with practical examples.

Let us start…

Pandas DataFrame

Before getting into conversion methods, let’s briefly understand what a Pandas DataFrame is.

A DataFrame is a two-dimensional, size-mutable, and heterogeneous tabular data structure with labeled axes (rows and columns). Think of it as a spreadsheet or SQL table where data is organized in rows and columns.

Read Add Rows to a DataFrame Pandas in a Loop in Python

Convert A Pandas DataFrame To A List In Python

Let me explain to you the methods to convert a Pandas DataFrame to a list in Python.

Method 1: Use values.tolist()

The easiest method to convert a DataFrame to a list is to use the values attribute combined with the tolist() method in Python.

import pandas as pd

# Create a sample DataFrame with US sales data
data = {
    'State': ['California', 'Texas', 'New York', 'Florida'],
    'Sales': [45000, 36000, 42000, 38000],
    'Quarter': ['Q1', 'Q1', 'Q2', 'Q2']
}

df = pd.DataFrame(data)

# Convert the entire DataFrame to a list of lists
result = df.values.tolist()
print(result)

Output:

[['California', 45000, 'Q1'], ['Texas', 36000, 'Q1'], ['New York', 42000, 'Q2'], ['Florida', 38000, 'Q2']]

I executed the above example code and added the screenshot below.

dataframe to list

This method converts each row of the DataFrame into a list, resulting in a list of lists.

Check out Convert Python Dictionary to Pandas DataFrame

Method 2: Use DataFrame.to_numpy() + tolist()

Another approach is to convert the DataFrame to a NumPy array first and then to a list in Python.

# Convert to NumPy array and then to list
result = df.to_numpy().tolist()
print(result)

Output:

[['California', 45000, 'Q1'], ['Texas', 36000, 'Q1'], ['New York', 42000, 'Q2'], ['Florida', 38000, 'Q2']]

I executed the above example code and added the screenshot below.

df to list

This method is particularly useful when you’re already working with NumPy arrays in your project.

Read Pandas str.replace Multiple Values in Python

Method 3: Convert a Single Column to a List

Often, you only need to convert a specific column to a list rather than the entire DataFrame in Python.

# Convert a single column to a list
states_list = df['State'].tolist()
print(states_list)

Output:

['California', 'Texas', 'New York', 'Florida']

I executed the above example code and added the screenshot below.

pandas to list

This is particularly useful when you need just one column of data, such as when creating labels for a chart or passing values to another function.

Check out Pandas Find Duplicates in Python

Method 4: Use list() Function

You can also use the Python built-in list() function to convert a DataFrame column to a list.

# Using list() function
sales_list = list(df['Sales'])
print(sales_list)

Output:

[45000, 36000, 42000, 38000]

This method is slightly more concise but functionally equivalent to the previous approach for single columns.

Method 5: Convert Specific Rows to Lists

Sometimes you might need to convert specific rows to Python lists rather than the entire DataFrame.

# Convert specific rows to lists
first_row = df.iloc[0].tolist()
print(first_row)

# Multiple specific rows
selected_rows = df.iloc[[0, 2]].values.tolist()
print(selected_rows)

Output:

['California', 45000, 'Q1']
[['California', 45000, 'Q1'], ['New York', 42000, 'Q2']]

This approach is useful when you’re interested in only certain rows of your DataFrame.

Convert to a Flattened List

If you need a completely flattened list (all values in a single list), you can use this approach:

# Convert to a flattened list
flat_list = df.values.flatten().tolist()
print(flat_list)

Output:

['California', 45000, 'Q1', 'Texas', 36000, 'Q1', 'New York', 42000, 'Q2', 'Florida', 38000, 'Q2']

This method is helpful when you need all values in a single, continuous list rather than maintaining the row structure.

Read Pandas Merge Fill NAN with 0 in Python

Performance Considerations

When working with large datasets, performance becomes important. I will give a quick comparison of these methods in terms of efficiency:

  1. df.values.tolist(): Fast and efficient for most use cases
  2. df.to_numpy().tolist(): Comparable to the first method, sometimes slightly faster for larger DataFrames
  3. df['Column'].tolist(): Efficient for single-column conversion
  4. list(df['Column']): Slightly less efficient than the direct tolist() method
  5. df.values.flatten().tolist(): Most resource-intensive as it restructures the data completely

For DataFrames with millions of rows, I recommend using method 1 or 2 as they leverage optimized NumPy operations under the hood.

Real-World Example: Analyze US Sales Data

Let’s look at a more comprehensive example where we analyze quarterly sales data for different US states and convert various parts of our analysis to lists for visualization.

import pandas as pd
import matplotlib.pyplot as plt

# Create a more comprehensive DataFrame
data = {
    'State': ['California', 'Texas', 'New York', 'Florida', 'Illinois', 
              'Pennsylvania', 'Ohio', 'Georgia', 'Michigan', 'North Carolina'],
    'Q1_Sales': [45000, 36000, 42000, 38000, 30000, 28000, 25000, 27000, 24000, 26000],
    'Q2_Sales': [48000, 39000, 44000, 40000, 32000, 29000, 26000, 29000, 25000, 28000],
    'Q3_Sales': [42000, 35000, 40000, 35000, 28000, 26000, 24000, 25000, 22000, 24000],
    'Q4_Sales': [52000, 42000, 48000, 45000, 35000, 32000, 28000, 30000, 26000, 29000]
}

df = pd.DataFrame(data)

# Get lists of states and Q4 sales for top performers
top_states = df.sort_values('Q4_Sales', ascending=False).head(5)['State'].tolist()
top_sales = df.sort_values('Q4_Sales', ascending=False).head(5)['Q4_Sales'].tolist()

print("Top performing states in Q4:", top_states)
print("Their respective sales figures:", top_sales)

# Calculate total annual sales and convert to list
df['Annual_Sales'] = df['Q1_Sales'] + df['Q2_Sales'] + df['Q3_Sales'] + df['Q4_Sales']
annual_sales_by_state = df[['State', 'Annual_Sales']].values.tolist()

print("\nAnnual sales by state:")
for state, sales in annual_sales_by_state:
    print(f"{state}: ${sales:,}")

Output:

Top performing states in Q4: ['California', 'New York', 'Florida', 'Texas', 'Illinois']
Their respective sales figures: [52000, 48000, 45000, 42000, 35000]

Annual sales by state:
California: $187,000
Texas: $152,000
New York: $174,000
Florida: $158,000
Illinois: $125,000
Pennsylvania: $115,000
Ohio: $103,000
Georgia: $111,000
Michigan: $97,000
North Carolina: $107,000

This example demonstrates how converting DataFrames or parts of DataFrames to lists can facilitate data analysis and reporting.

Check out Pandas GroupBy Without Aggregation Function in Python

When to Use DataFrame to List Conversion

Based on my experience, here are some common scenarios where converting a DataFrame to a list is particularly useful:

  1. When working with visualization libraries that expect list inputs
  2. When passing data to JavaScript through JSON (especially in web applications)
  3. For compatibility with legacy code that works with lists
  4. When performing operations that are more efficient with Python’s built-in list methods
  5. When you need to simplify the data structure for easier manipulation

I hope you found this tutorial helpful. The methods that I explained in this tutorial use values.tolist(), DataFrame.to_numpy() + tolist(), list() Function, convert a single column to a list, and specific rows to lists.

You may 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.