HOWTO · Python
How to Convert List to Pandas DataFrame in Python
This tutorial introduces how to convert a list to a Pandas DataFrame in Python
On this page
Convert a Python list to a Pandas DataFrame with pd.DataFrame(my_list). A one-dimensional list becomes one column with the default integer row index. Use the columns argument to name that column, index to supply row labels, or a list of lists to create multiple columns.
Convert List to a Pandas DataFrame in Python
A DataFrame is a two-dimensional table with labeled rows and columns. Import Pandas, pass the list to its DataFrame constructor, and assign the result to a variable. In a script, call print(df) to display it; in a notebook or interactive session, evaluating df displays the table.
This example puts each name in the single column labeled 0. Pandas assigns row labels starting at 0 by default. pd.DataFrame() is the constructor that creates the table; it is not a method called on the list. This distinction matters when adapting the call: pass the data as its first argument, then use named arguments such as columns or index to describe the table.
import pandas as pd
my_list = ["Tom", "Mark", "Tony"]
df = pd.DataFrame(my_list)
print(df)
Store the List in a Column in Pandas DataFrame in Python
Pass a one-item list of labels to the columns argument when constructing the DataFrame. The label names the column; it does not change the row index. The number of column labels must match the number of columns in the input data.
Here, the values remain in their original order, and Names replaces the default column label 0.
import pandas as pd
my_list = ["Tom", "Mark", "Tony"]
df = pd.DataFrame(my_list, columns=["Names"])
print(df)
Convert a List to a DataFrame With Index in Python
Use the index argument to set row labels while columns sets column labels. Supply one index label for each list value so each row has a corresponding label. These labels replace the default integer index; they do not become another data column. For example, the first value is identified by i., the second by ii., and the third by iii.. Use labels that make sense for your records, such as dates or product identifiers.
import pandas as pd
my_list = ["Tom", "Mark", "Tony"]
df = pd.DataFrame(
my_list,
index=["i.", "ii.", "iii."],
columns=["Names"],
)
print(df)
Zip Two Lists Into a Single DataFrame Using zip() in Python
Python’s zip() pairs values from input iterables by position. Pass the pairs to pd.DataFrame() and provide one column label for each input list. The example uses parallel lists of names and heights, so each name stays on the same row as its height. You can pair more than two lists by passing them all to zip() and supplying the matching column labels.
zip() stops as soon as the shortest input is exhausted. If each source list is expected to contribute one value to every row, make sure their lengths match before creating the DataFrame.
import pandas as pd
name_list = ["Tom", "Mark", "Tony"]
height_list = [150, 151, 152]
rows = zip(name_list, height_list)
df = pd.DataFrame(rows, columns=["Name", "Height"])
print(df)
Convert a Multi-Dimensional List to a Pandas DataFrame in Python
For tabular data, represent each row as an inner list and pass the outer list to pd.DataFrame(). Give columns one label for each value in a row. In this example, every inner list contains a name followed by an age, so the table has two columns and three rows.
This shape is suitable when each row has the same fields in the same order. Check that the row values line up with the labels you provide; for records with different fields, normalize the data into a consistent tabular shape first.
import pandas as pd
info = [["Tom", 18], ["Mark", 25], ["Tony", 68]]
df = pd.DataFrame(info, columns=["Name", "Age"])
print(df)