How to convert a list to a Pandas DataFrame with Python?

When analyzing and visualizing data with Pandas there might be cases that you would like to use data stored in Python lists.

In today’s post, we’ll learn the basics of bringing together lists and DataFrames in Pandas. We’ll specifically cover the following key cases:

Pandas lists into DataFrames

  • Initialize and populate a DataFrame with a list.
  • Append a list to a DataFrame column using df.insert()
  • Insert a list as a New DataFrame row
  • Create a pandas DataFrame from a list of lists

Let’s get going:

1. Defining the test Data

# import Data Analysis libraries
import pandas as pd

# Define data
languages = ['Ruby', 'Go', 'Visual Basic', 'SQL', 'Python', 'Ruby', 'C++']
salary = [124.0, 133.0, 122.0, 107.0, 131.0, 134.0, 108.0]
employees = [10,15,18,14,16,18,19]

2. New DataFrame from Python list

# Python list to Df

languages_df = pd.DataFrame(languages, columns=['language'])
display(languages_df)

We’ll use the pd.DataFrame() method to pass list data. We’ll start by passing a single list, but as you’ll immediately see, we can pass multiple as needed.

Here’s our DF.

language
0Ruby
1Go
2Visual Basic
3SQL
4Python
5Ruby
6C++

3. Insert Python list as a column with insert()

We’ll now add an additional list to the existing DataFrame. We’ll use the num_employees list that we previously defined.

We’ll use the loc parameter to specify the column order. Note that we can control the column order as shown in this post.

#Python list to DataFrame column
languages_df.insert(loc=1, column='num_employees', value = employees)

Here’s the output:

languagenum_employees
0Ruby10
1Go15
2Visual Basic18
3SQL14
4Python16
5Ruby18
6C++19

4. Existing list as new row

# Defining the new row
new_row = ['Python', 12]

languages_df.loc[len(languages_df)] = new_row

This code will add a new row and the last row of the DataFRame.

5. Pandas DataFrame from list of lists

We’ll do this in two steps:

  • First, we will use the coll zip() function to stitch two or more lists together into a zip object and transform the zip object to a list.
  • We’ll then create the new DataFrame as shown in the examples above:
my_zip_list = list (zip(languages, salary))
my_zip_list

Here’s our list of tuples:

[('Ruby', 124.0),
 ('Go', 133.0),
 ('Visual Basic', 122.0),
 ('SQL', 107.0),
 ('Python', 131.0),
 ('Ruby', 134.0),
 ('C++', 108.0)]

Next we’ll pass the list as an argument into the DF constructor:

my_new_df = pd.DataFrame(list (zip(languages, salary)), columns = ['languages', 'salary'])
my_new_df

Here’s the result:


languages
salary
0Ruby124.0
1Go133.0
2Visual Basic122.0
3SQL107.0
4Python131.0
5Ruby134.0
6C++108.0