Python Pandas: How to Convert a Pandas DataFrame into a List
Converting a pandas DataFrame into a Python list is a common operation when you need to pass data to functions that expect lists, serialize data for APIs, export to non-pandas formats, or simply work with standard Python data structures.
Pandas provides several ways to perform this conversion, and the right method depends on what structure you need - a flat list from a single column, a nested list of rows, a list of columns, or a list that includes column headers. In this guide, you'll learn all these approaches with clear examples.
Setting Up the Example DataFrame
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'Age': [25, 30, 35, 28],
'Score': [88.5, 92.3, 76.8, 95.1]
})
print(df)
Output:
Name Age Score
0 Alice 25 88.5
1 Bob 30 92.3
2 Charlie 35 76.8
3 Diana 28 95.1
Converting a Single Column to a List
Use tolist() on a column (Series) to get a flat list of its values:
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'Age': [25, 30, 35, 28],
'Score': [88.5, 92.3, 76.8, 95.1]
})
names = df['Name'].tolist()
ages = df['Age'].tolist()
print(f"Names: {names}")
print(f"Ages: {ages}")
print(f"Type: {type(names)}")
Output:
Names: ['Alice', 'Bob', 'Charlie', 'Diana']
Ages: [25, 30, 35, 28]
Type: <class 'list'>
tolist() converts values to native Python types (int, float, str), making the result safe for JSON serialization and other operations that don't support NumPy types.
Converting a DataFrame to a List of Rows
Each row becomes a sublist containing all column values. Use df.values.tolist():
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'Age': [25, 30, 35, 28],
'Score': [88.5, 92.3, 76.8, 95.1]
})
rows = df.values.tolist()
print(rows)
Output:
[['Alice', 25, 88.5], ['Bob', 30, 92.3], ['Charlie', 35, 76.8], ['Diana', 28, 95.1]]
For better readability:
for row in rows:
print(row)
Output:
['Alice', 25, 88.5]
['Bob', 30, 92.3]
['Charlie', 35, 76.8]
['Diana', 28, 95.1]
Converting a DataFrame to a List of Columns
To get each column as a separate list (grouped by column rather than by row):
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'Age': [25, 30, 35, 28],
'Score': [88.5, 92.3, 76.8, 95.1]
})
columns_list = [df[col].tolist() for col in df.columns]
for col_name, col_data in zip(df.columns, columns_list):
print(f"{col_name}: {col_data}")
Output:
Name: ['Alice', 'Bob', 'Charlie', 'Diana']
Age: [25, 30, 35, 28]
Score: [88.5, 92.3, 76.8, 95.1]
Including Column Names in the List
To create a list where the first sublist contains column headers:
result = [df.columns.tolist()] + df.values.tolist()
for row in result:
print(row)
Output:
['Name', 'Age', 'Score']
['Alice', 25, 88.5]
['Bob', 30, 92.3]
['Charlie', 35, 76.8]
['Diana', 28, 95.1]
This format is useful when exporting data to CSV writers or spreadsheet libraries that expect headers as the first row.
Converting to a List of Dictionaries
When you need each row as a dictionary (common for JSON APIs and database operations):
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'Age': [25, 30, 35, 28],
'Score': [88.5, 92.3, 76.8, 95.1]
})
records = df.to_dict('records')
for record in records:
print(record)
Output:
{'Name': 'Alice', 'Age': 25, 'Score': 88.5}
{'Name': 'Bob', 'Age': 30, 'Score': 92.3}
{'Name': 'Charlie', 'Age': 35, 'Score': 76.8}
{'Name': 'Diana', 'Age': 28, 'Score': 95.1}
to_dict('records') is the go-to method when converting DataFrame data for JSON serialization or passing to APIs. Each row becomes a self-describing dictionary with column names as keys.
Converting to a List of Tuples
When you need immutable row data (useful for database insertions or set operations):
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'Age': [25, 30, 35, 28],
'Score': [88.5, 92.3, 76.8, 95.1]
})
tuples = [tuple(row) for row in df.values]
print(tuples)
Output:
[('Alice', 25, 88.5), ('Bob', 30, 92.3), ('Charlie', 35, 76.8), ('Diana', 28, 95.1)]
Alternatively, use itertuples() for a more memory-efficient approach:
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'Age': [25, 30, 35, 28],
'Score': [88.5, 92.3, 76.8, 95.1]
})
tuples = [(row.Name, row.Age, row.Score) for row in df.itertuples()]
print(tuples)
Output:
[('Alice', 25, 88.5), ('Bob', 30, 92.3), ('Charlie', 35, 76.8), ('Diana', 28, 95.1)]
Converting the Index to a List
import pandas as pd
df = pd.DataFrame(
{'Score': [88, 92, 76]},
index=['Alice', 'Bob', 'Charlie']
)
index_list = df.index.tolist()
print(f"Index as list: {index_list}")
Output:
Index as list: ['Alice', 'Bob', 'Charlie']
Converting Specific Rows to a List
Use slicing or boolean filtering to convert only certain rows:
import pandas as pd
df = pd.DataFrame({
'Name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'Age': [25, 30, 35, 28],
'Score': [88.5, 92.3, 76.8, 95.1]
})
# First 2 rows
first_two = df.head(2).values.tolist()
print(f"First 2 rows: {first_two}")
# Rows where Score > 90
high_scorers = df[df['Score'] > 90].values.tolist()
print(f"High scorers: {high_scorers}")
Output:
First 2 rows: [['Alice', 25, 88.5], ['Bob', 30, 92.3]]
High scorers: [['Bob', 30, 92.3], ['Diana', 28, 95.1]]
Practical Example: Preparing Data for Export
import pandas as pd
import json
df = pd.DataFrame({
'Product': ['Laptop', 'Phone', 'Tablet'],
'Price': [999, 699, 329],
'Stock': [50, 120, 85]
})
# For CSV writers (list of lists with header)
csv_data = [df.columns.tolist()] + df.values.tolist()
print("CSV format:")
for row in csv_data:
print(row)
# For JSON APIs (list of dictionaries)
json_data = df.to_dict('records')
print(f"\nJSON format:\n{json.dumps(json_data, indent=2)}")
Output:
CSV format:
['Product', 'Price', 'Stock']
['Laptop', 999, 50]
['Phone', 699, 120]
['Tablet', 329, 85]
JSON format:
[
{
"Product": "Laptop",
"Price": 999,
"Stock": 50
},
{
"Product": "Phone",
"Price": 699,
"Stock": 120
},
{
"Product": "Tablet",
"Price": 329,
"Stock": 85
}
]
Quick Comparison of Methods
| Goal | Method | Output Structure |
|---|---|---|
| Single column → flat list | df['col'].tolist() | [val1, val2, ...] |
| All rows → list of lists | df.values.tolist() | [[row1], [row2], ...] |
| Columns → list of lists | [df[c].tolist() for c in df.columns] | [[col1], [col2], ...] |
| With headers | [df.columns.tolist()] + df.values.tolist() | [[headers], [row1], ...] |
| Rows → list of dicts | df.to_dict('records') | [{row1}, {row2}, ...] |
| Rows → list of tuples | [tuple(r) for r in df.values] | [(row1), (row2), ...] |
| Index → list | df.index.tolist() | [idx1, idx2, ...] |
Conclusion
Pandas provides flexible methods to convert DataFrames into various list formats:
- Use
df['column'].tolist()to extract a single column as a flat list. - Use
df.values.tolist()to convert the entire DataFrame into a list of row-based sublists. - Use
df.to_dict('records')for a list of dictionaries - ideal for JSON APIs and data interchange. - Use
[df.columns.tolist()] + df.values.tolist()when you need column headers as the first row.
Choose the format that matches your downstream requirement - whether it's CSV export, JSON serialization, database insertion, or passing data to non-pandas functions.