How to convert a Python list or lists to a pandas DataFrame

The pandas library and DataFrame

pandas is a data analysis software package in Python, which is used for manipulating data. This library contains the DataFrame class.

A pandas DataFrame is a 2D data structure, which is organized in rows and columns in a tabular format. It allows us to store tabular data and perform various operations on that data using different methods.

Creating a DataFrame from a Python list

A Python list is an ordered and changeable collection of data objects. We can create a pandas DataFrame from a Python list. Let's look at the following code to see how we can achieve this.

Syntax

The code snippet given below demonstrates the creation of a pandas DataFrame from a Python list.

df = pandas.DataFrame(list)

Parameter values

  • pandas: This is the library that contains the DataFrame class. The line shown above calls the constructor for the DataFrame class.
  • list: This is a required parameter that needs to be passed to the constructor.

Return value

The DataFrame constructor returns a pandas DataFrame that contains all the elements in a list. In the code snippet given above, the DataFrame instance is stored in df.

Example

Let's look at an example that uses the syntax shown above:

# Importing pandas
import pandas as pd
# creating a list
ls = [1,2,3,4,5]
#creating dataframe from list
df = pd.DataFrame(ls)
# printing df
print(df)
# creating a list
ls = [[1,2,3], [4,5,6], [7,8,9]]
#creating dataframe from list
df = pd.DataFrame(ls)
# printing df
print(df)

Explanation

  • Line 2: We import pandas as pd.
  • Line 4: We create a Python list called ls.
  • Line 6: We create a Pandas DataFrame called df from the ls list.
  • Line 8: We print the df DataFrame. We observe that df contains all the elements from ls.
  • Line 10: We update ls to make it a list that contains other lists.
  • Line 12: We create a pandas DataFrame called df from the ls list.
  • Line 14: We print the df DataFrame. We observe that df contains all the elements from ls.

    Free Resources

    Copyright ©2025 Educative, Inc. All rights reserved