How to Convert List of Lists to a Pandas Dataframe

Problem: You’re given a list of lists. Your goal is to convert it into a Pandas Dataframe.

Example: Say, you want to compare salary data of different companies and job descriptions. You’ve obtained the following salary data set as a list of list:

salary = [['Google', 'Machine Learning Engineer', 121000],
          ['Google', 'Data Scientist', 109000],
          ['Google', 'Tech Lead', 129000],
          ['Facebook', 'Data Scientist', 103000]]

How can you convert this into a Pandas Dataframe?

DataFrame()

Solution: The straight-forward solution is to use the pandas.DataFrame() constructor that creates a new Dataframe object from different input types such as NumPy arrays or lists.

Here’s how to do it for the given example:

import pandas as pd

salary = [['Google', 'Machine Learning Engineer', 121000],
          ['Google', 'Data Scientist', 109000],
          ['Google', 'Tech Lead', 129000],
          ['Facebook', 'Data Scientist', 103000]]

df = pd.DataFrame(salary)

This results in the following Dataframe:

print(df)

'''
          0                          1       2
0    Google  Machine Learning Engineer  121000
1    Google             Data Scientist  109000
2    Google                  Tech Lead  129000
3  Facebook             Data Scientist  103000
'''

Try It Yourself: Run this code in our interactive Python shell by clicking the “Run” button.

DataFrame.from_records()

An alternative is the pandas.DataFrame.from_records() method that generates the same output:

import pandas as pd

salary = [['Company', 'Job', 'Salary($)'],
          ['Google', 'Machine Learning Engineer', 121000],
          ['Google', 'Data Scientist', 109000],
          ['Google', 'Tech Lead', 129000],
          ['Facebook', 'Data Scientist', 103000]]

df = pd.DataFrame.from_records(salary)
print(df)
'''
          0                          1       2
0    Google  Machine Learning Engineer  121000
1    Google             Data Scientist  109000
2    Google                  Tech Lead  129000
3  Facebook             Data Scientist  103000
'''

Try It Yourself: Run this code in our interactive Python shell by clicking the “Run” button.

Column Names

If you want to add column names to make the output prettier, you can also pass those as a separate argument:

import pandas as pd

salary = [['Google', 'Machine Learning Engineer', 121000],
          ['Google', 'Data Scientist', 109000],
          ['Google', 'Tech Lead', 129000],
          ['Facebook', 'Data Scientist', 103000]]

df = pd.DataFrame(salary, columns=['Company', 'Job', 'Salary($)'])
print(df)

'''
    Company                        Job  Salary($)
0    Google  Machine Learning Engineer     121000
1    Google             Data Scientist     109000
2    Google                  Tech Lead     129000
3  Facebook             Data Scientist     103000
'''

Try It Yourself: Run this code in our interactive Python shell by clicking the “Run” button.

If the first list of the list of lists contains the column name, use slicing to separate the first list from the other lists:

import pandas as pd

salary = [['Company', 'Job', 'Salary($)'],
          ['Google', 'Machine Learning Engineer', 121000],
          ['Google', 'Data Scientist', 109000],
          ['Google', 'Tech Lead', 129000],
          ['Facebook', 'Data Scientist', 103000]]

df = pd.DataFrame(salary[1:], columns=salary[0])
print(df)

'''
    Company                        Job  Salary($)
0    Google  Machine Learning Engineer     121000
1    Google             Data Scientist     109000
2    Google                  Tech Lead     129000
3  Facebook             Data Scientist     103000
'''

Slicing is a powerful Python feature and before you can master Pandas, you need to master slicing. To refresh your Python slicing skills, download my ebook “Coffee Break Python Slicing” for free.

Summary: To convert a list of lists into a Pandas DataFrame, use the pd.DataFrame() constructor and pass the list of lists as an argument. An optional columns argument can help you structure the output.

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!