In this short guide, you'll see how to** work with lists in pandas apply() function when pd.Series() doesn't expand properly.**
Here you can find the short answer:
(1) Using .tolist() with pd.DataFrame()
df[['col1', 'col2', 'col3']] = pd.DataFrame(df['list_column'].tolist(), index=df.index)
(2) Using apply with result_type='expand'
df[['col1', 'col2']] = df['list_column'].apply(pd.Series)
(3) Using list comprehension
expanded = pd.DataFrame([x for x in df['list_column']], columns=['A', 'B', 'C'])
So let's see how to properly expand lists into separate columns in pandas DataFrames.
Suppose you have data like:
| ID | Coordinates |
|---|---|
| 1 | [10.5, 20.3, 5.2] |
| 2 | [15.7, 25.1, 8.4] |
| 3 | [12.3, 22.8, 6.9] |
1: Expand lists using tolist() method (Recommended)
Let's start with the most reliable method - using .tolist() with pd.DataFrame() constructor:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'customer': ['Apple Inc', 'Microsoft Corp', 'Google LLC'],
'metrics': [[100, 85, 92], [95, 88, 90], [98, 91, 87]]
})
df[['sales', 'satisfaction', 'retention']] = pd.DataFrame(
df['metrics'].tolist(),
index=df.index
)
print(df)
result will be:
customer metrics sales satisfaction retention
0 Apple Inc [100, 85, 92] 100 85 92
1 Microsoft Corp [95, 88, 90] 95 88 90
2 Google LLC [98, 91, 87] 98 91 87
The .tolist() method converts the Series of lists into a Python list, which pd.DataFrame() can then properly expand into columns. This approach is fast, reliable, and handles index alignment automatically.
What if your lists have different lengths? The method handles it gracefully with NaN:
df = pd.DataFrame({
'company': ['Amazon', 'Tesla', 'Meta'],
'quarterly_revenue': [[120, 135, 150], [80, 95], [110, 125, 140, 155]]
})
df[['Q1', 'Q2', 'Q3', 'Q4']] = pd.DataFrame(
df['quarterly_revenue'].tolist(),
index=df.index
)
print(df[['company', 'Q1', 'Q2', 'Q3', 'Q4']])
result:
company Q1 Q2 Q3 Q4
0 Amazon 120.0 135.0 150.0 NaN
1 Tesla 80.0 95.0 NaN NaN
2 Meta 110.0 125.0 140.0 155.0
2: Using apply(pd.Series) with proper syntax
The apply(pd.Series) approach works but requires understanding its limitations and proper usage:
import pandas as pd
df = pd.DataFrame({
'product': ['iPhone 15', 'Galaxy S24', 'Pixel 8'],
'rgb_color': [[255, 0, 0], [0, 255, 0], [0, 0, 255]]
})
color_df = df['rgb_color'].apply(pd.Series)
color_df.columns = ['Red', 'Green', 'Blue']
df = pd.concat([df, color_df], axis=1)
print(df[['product', 'Red', 'Green', 'Blue']])
result:
product Red Green Blue
0 iPhone 15 255 0 0
1 Galaxy S24 0 255 0
2 Pixel 8 0 0 255
Important notes:
apply(pd.Series)works only on Series, not on DataFrame columns directly in assignment- You need to rename columns afterward if you want meaningful names
- This method is slower than
.tolist()for large datasets
3: Handle nested structures with list comprehension
For more complex nested structures or when you need custom processing, use list comprehension:
import pandas as pd
df = pd.DataFrame({
'employee': ['Sarah Johnson', 'Michael Chen', 'Emma Davis'],
'skills': [
['Python', 'SQL', 'Tableau'],
['Java', 'AWS'],
['React', 'TypeScript', 'Node.js', 'Docker']
]
})
expanded = pd.DataFrame([
{f'skill_{i+1}': skill for i, skill in enumerate(row)}
for row in df['skills']
])
result = pd.concat([df[['employee']], expanded], axis=1)
print(result)
result:
employee skill_1 skill_2 skill_3 skill_4
0 Sarah Johnson Python SQL Tableau NaN
1 Michael Chen Java AWS NaN NaN
2 Emma Davis React TypeScript Node.js Docker
This approach gives you full control over the expansion process and allows custom transformations during the conversion.