0
0
Pandasdata~5 mins

Creating DataFrame from list of lists in Pandas

Choose your learning style9 modes available
Introduction

We use DataFrames to organize data in rows and columns, like a table. Creating a DataFrame from a list of lists helps turn simple lists into a structured table for easy analysis.

You have raw data as lists inside a bigger list and want to analyze it.
You want to convert spreadsheet-like data stored as lists into a DataFrame.
You need to prepare data for plotting or statistics from nested lists.
You want to add column names to simple list data for clarity.
Syntax
Pandas
import pandas as pd

data = [
    [value1_row1, value2_row1],
    [value1_row2, value2_row2]
]
df = pd.DataFrame(data, columns=["Column1", "Column2"])

The outer list holds rows; each inner list is a row's values.

Column names are optional but help identify data clearly.

Examples
This creates an empty DataFrame with no rows or columns.
Pandas
import pandas as pd

# Empty list
empty_data = []
df_empty = pd.DataFrame(empty_data)
print(df_empty)
DataFrame with a single row and named columns.
Pandas
import pandas as pd

# One row only
data_one_row = [[10, 20, 30]]
df_one_row = pd.DataFrame(data_one_row, columns=["A", "B", "C"])
print(df_one_row)
DataFrame with default numeric column names (0, 1).
Pandas
import pandas as pd

# Multiple rows, no column names
data_multiple = [[1, 2], [3, 4], [5, 6]]
df_multiple = pd.DataFrame(data_multiple)
print(df_multiple)
DataFrame with string and integer columns.
Pandas
import pandas as pd

# Different data types
data_mixed = [["Alice", 25], ["Bob", 30]]
df_mixed = pd.DataFrame(data_mixed, columns=["Name", "Age"])
print(df_mixed)
Sample Program

This program shows how to turn a list of lists into a DataFrame with column names. It prints the original list and the resulting DataFrame.

Pandas
import pandas as pd

# Create list of lists data
student_scores = [
    ["John", 85, 92],
    ["Emma", 78, 88],
    ["Liam", 90, 95]
]

# Print original list
print("Original list of lists:")
print(student_scores)

# Create DataFrame with column names
df_scores = pd.DataFrame(student_scores, columns=["Student", "Math", "Science"])

# Print DataFrame before any changes
print("\nDataFrame created from list of lists:")
print(df_scores)
OutputSuccess
Important Notes

Creating a DataFrame from a list of lists takes O(n) time where n is number of rows.

It uses extra space to store the DataFrame structure besides the original list.

Common mistake: forgetting to provide column names can make data harder to understand.

Use this method when starting from raw nested list data; use other methods if data is in dicts or files.

Summary

DataFrames organize data in rows and columns for easy analysis.

List of lists is a simple way to create DataFrames from raw data.

Always add column names to make your data clear and readable.