How to create a Pandas DataFrame from multiple lists?

In this short tutorial we’ll learn how to use the Python and the Pandas library to easily combine and convert several lists into a DataFrame. In this example we’ll show how to combine the lists together so that each list will represent a different column in the DataFrame.

Multiple list to DataFrame example

We’ll look into two different ways to convert our lists of lists to a Python DataFrame object:

  1. Zipping the lists and then constructing the DF.
  2. Defining a dictionary first and then constructing the DataFrame.

Preparatory steps

We’ll quickly import the Pandas library into our Python development environment and then define two list objects that we’ll use in this example:

import pandas as pd

office = ['Montreal', 'Toronto', 'Dallas', 'NYC']
avg_salary = [190, 180, 220, 290]

Using zip to append lists and then building the DF

If we want to get each list data into different columns, then we have to define a list of lists first. Each list will represent a row in the DataFrame. The super handy zip function allows us to stitch the lists together.

hr_lst  = list (zip(office, avg_salary))
hr_lst

We’ll get a list of lists:

[('Montreal', 190), ('Toronto', 180), ('Dallas', 220), ('NYC', 290)]

We’ll now construct the DataFrame object:

hr_df_1 = pd.DataFrame(hr_lst, columns = ['office', 'salary'])

hr_df_1.head()

Here’s the result:

officesalary
0Montreal190
1Toronto180
2Dallas220
3NYC290

Dictionary from multiple lists and the converting to DataFrame

The second method we’ll quickly use to construct our DF from multiple list objects is using the pd.DataFrame.from_dict method.

We’ll first define a simple dictionary using the dict() function

hr_dict = dict(office=office, salary =avg_salary)

We’ll get the following dictionary:

{'office': ['Montreal', 'Toronto', 'Dallas', 'NYC'],
 'salary': [190, 180, 220, 290]}

We’ll now call pd.DataFrame.from_dict method:

hr_df_2 = pd.DataFrame.from_dict(hr_dict)

hr_df_2.head()

The result is similar as before (note that in this case there is no need to specify the column names, those are inferrred automatically from the dictionary.

officesalary
0Montreal190
1Toronto180
2Dallas220
3NYC290