Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Create a Pandas DataFrame from lists
A Pandas DataFrame is a two-dimensional table-like data structure with labeled rows and columns. Creating DataFrames from lists is one of the most fundamental operations in pandas, allowing you to transform Python lists into structured data for analysis.
This article demonstrates various methods to create pandas DataFrames from lists with practical examples.
Basic DataFrame Creation from a Single List
The simplest way to create a DataFrame is from a single list ?
import pandas as pd names = ['Alice', 'Bob', 'Charlie', 'Diana'] df = pd.DataFrame(names, columns=['Name']) print(df)
Name
0 Alice
1 Bob
2 Charlie
3 Diana
Creating DataFrame from Multiple Lists
Method 1: Using Dictionary of Lists
This is the most common and readable approach ?
import pandas as pd
names = ['Alice', 'Bob', 'Charlie', 'Diana']
ages = [25, 30, 35, 28]
cities = ['New York', 'London', 'Tokyo', 'Paris']
df = pd.DataFrame({
'Name': names,
'Age': ages,
'City': cities
})
print(df)
Name Age City
0 Alice 25 New York
1 Bob 30 London
2 Charlie 35 Tokyo
3 Diana 28 Paris
Method 2: Using zip() Function
Combine multiple lists using zip() to create rows ?
import pandas as pd
names = ['Alice', 'Bob', 'Charlie', 'Diana']
ages = [25, 30, 35, 28]
cities = ['New York', 'London', 'Tokyo', 'Paris']
df = pd.DataFrame(list(zip(names, ages, cities)),
columns=['Name', 'Age', 'City'])
print(df)
Name Age City
0 Alice 25 New York
1 Bob 30 London
2 Charlie 35 Tokyo
3 Diana 28 Paris
Method 3: Using List of Lists
Create DataFrame from a list containing multiple lists as rows ?
import pandas as pd
data = [
['Alice', 25, 'New York'],
['Bob', 30, 'London'],
['Charlie', 35, 'Tokyo'],
['Diana', 28, 'Paris']
]
df = pd.DataFrame(data, columns=['Name', 'Age', 'City'])
print(df)
Name Age City
0 Alice 25 New York
1 Bob 30 London
2 Charlie 35 Tokyo
3 Diana 28 Paris
DataFrame with Mixed Data Types
DataFrames can handle different data types in each column ?
import pandas as pd
employees = {
'Name': ['John', 'Sarah', 'Mike'],
'Age': [32, 28, 45],
'Salary': [50000.50, 65000.75, 80000.00],
'Active': [True, False, True]
}
df = pd.DataFrame(employees)
print(df)
print("\nData types:")
print(df.dtypes)
Name Age Salary Active 0 John 32 50000.5 True 1 Sarah 28 65000.8 False 2 Mike 45 80000.0 True Data types: Name object Age int64 Salary float64 Active bool dtype: object
Comparison of Methods
| Method | Readability | Best For | Performance |
|---|---|---|---|
| Dictionary of Lists | High | Most cases | Good |
| Using zip() | Medium | Dynamic columns | Good |
| List of Lists | Medium | Row-oriented data | Best |
Key Points
Column names ? Always specify column names for clarity
Data alignment ? All lists must have the same length
Index ? DataFrame automatically creates a numeric index starting from 0
Data types ? Pandas automatically infers data types from the lists
Conclusion
Creating pandas DataFrames from lists is straightforward using dictionary of lists, zip(), or list of lists. The dictionary method is most readable and commonly used for structured data creation.
