How do i turn a list into a dataframe in python?

On this page you’ll learn how to create a pandas DataFrame from a list object in the Python programming language.

The page will consist of three examples for the creation of a pandas DataFrame from a list object. To be more precise, the content of the page looks as follows:

Let’s do this:

Creating Example Data

We use the following list object as a basis for this Python tutorial:

my_list = [1, 2, 3, 4, 5]                          # Create example list
print(my_list)                                     # Print example list
# [1, 2, 3, 4, 5]

As you can see, our list contains five integer elements ranging from the values 1 to 5.

Example 1: Convert List to pandas DataFrame Column

In Example 1, I’ll show how to construct a new pandas DataFrame based on a list using the Python programming language.

For the following tutorial, we’ll first need to import the pandas library:

import pandas as pd                                # Import pandas library to Python

In the next step, we can use the DataFrame function of the pandas library to convert our example list to a single column in a new pandas DataFrame:

my_data1 = pd.DataFrame({'x': my_list})            # Create pandas DataFrame from list
print(my_data1)                                    # Print pandas DataFrame

How do i turn a list into a dataframe in python?

Table 1 illustrates that our new pandas DataFrame is composed of five rows and one column. The values in this column correspond to the values in our list.

Example 2: Convert Each List Element to Separate Column of pandas DataFrame

In this example, I’ll show how to create a pandas DataFrame with a new variable for each element in a list.

We can do this in two steps. First, we have to initialize our pandas DataFrame using the DataFrame function. Second, we have to set the column names of our DataFrame.

Consider the Python syntax below:

my_data2 = pd.DataFrame([my_list])                 # Each list element as column
my_data2.columns = ['x1', 'x2', 'x3', 'x4', 'x5']  # Change column names
print(my_data2)                                    # Print pandas DataFrame

How do i turn a list into a dataframe in python?

By running the previous syntax, we have created Table 2, i.e. another data set containing one row and a separate column for each element in our list.

Example 3: Add List as New Column to Existing pandas DataFrame

This example explains how to append a list object as a new column to an already existing pandas DataFrame.

For this, we first have to create an exemplifying DataFrame:

my_data3 = pd.DataFrame({'x1':range(1, 6),         # Create pandas DataFrame
                         'x2':range(7, 2, - 1),
                         'x3':range(12, 17)})
print(my_data3)                                    # Print pandas DataFrame

How do i turn a list into a dataframe in python?

As shown in Table 3, we have created a new pandas DataFrame consisting of five rows and three columns.

Note that it’s important that the number of rows in this DataFrame are equal to the length of our list object.

In the next step, we can add our list object as a new variable to our pandas DataFrame:

my_data3['new_col'] = my_list                      # Add list to existing DataFrame
print(my_data3)                                    # Print pandas DataFrame

How do i turn a list into a dataframe in python?

As revealed in Table 4, the previous code has created an updated version of our input data set to which our list has been concatenated as a new variable.

Video & Further Resources

In case you need more information on the Python programming syntax of this article, you might watch the following video on my YouTube channel. I’m explaining the Python programming code of this article in the video.

Besides that, you could have a look at some of the related articles on my website.

  • Convert NumPy Array to pandas DataFrame in Python
  • Get Column Names of pandas DataFrame as List in Python
  • Convert pandas DataFrame Column to datetime in Python in R
  • Convert Index to Column of pandas DataFrame in Python
  • Convert pandas DataFrame to NumPy Array in Python
  • Convert pandas DataFrame Index to List & NumPy Array in Python
  • Handling DataFrames Using the pandas Library in Python
  • All Python Programming Tutorials

On this page you have learned how to convert a list to a pandas DataFrame in the Python programming language. In case you have any further questions or comments, please let me know in the comments.

How do I make a list into a data frame?

The pandas DataFrame can be created by using the list of lists, to do this we need to pass a python list of lists as a parameter to the pandas. DataFrame() function. Pandas DataFrame will represent the data in a tabular format, like rows and columns.

How do I turn a list into a table in python?

How to Easily Create Tables in Python.
install tabulate. We first install the tabulate library using pip install in the command line: pip install tabulate..
import tabulate function. ... .
list of lists. ... .
dictionary of iterables. ... .
missing values..

How do I turn my list into pandas?

Pandas series can be converted to a list using tolist() or type casting method. There can be situations when you want to perform operations on a list instead of a pandas object. In such cases, you can store the DataFrame columns in a list and perform the required operations.

What does Tolist () do in python?

The tolist() function is used to convert a given array to an ordinary list with the same items, elements, or values.