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.
Creating DataFrame from list of lists in 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.
import pandas as pd # Empty list empty_data = [] df_empty = pd.DataFrame(empty_data) print(df_empty)
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)
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)
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)
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.
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)
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.
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.