Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] How to Convert List of Lists to a Pandas Dataframe

#1
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!



https://www.sickgaming.net/blog/2020/04/...dataframe/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Python zip(): Get Elements from Multiple Lists xSicKxBot 0 1,967 08-26-2023, 01:28 AM
Last Post: xSicKxBot
  [Tut] Making $65 per Hour on Upwork with Pandas xSicKxBot 0 1,319 05-24-2023, 08:16 PM
Last Post: xSicKxBot
  [Tut] Pandas Series Object – A Helpful Guide with Examples xSicKxBot 0 1,307 05-01-2023, 01:30 AM
Last Post: xSicKxBot
  [Tut] Python List of Tuples to DataFrame ? xSicKxBot 0 1,508 04-22-2023, 06:10 AM
Last Post: xSicKxBot
  [Tut] Dictionary of Lists to DataFrame – Python Conversion xSicKxBot 0 1,374 04-17-2023, 03:46 AM
Last Post: xSicKxBot
  [Tut] Pandas Boolean Indexing xSicKxBot 0 1,305 04-16-2023, 10:54 AM
Last Post: xSicKxBot
  [Tut] Python List of Dicts to Pandas DataFrame xSicKxBot 0 1,525 04-11-2023, 04:15 AM
Last Post: xSicKxBot
  [Tut] How to Create a DataFrame From Lists? xSicKxBot 0 1,219 12-17-2022, 03:17 PM
Last Post: xSicKxBot
  [Tut] Easiest Way to Convert List of Hex Strings to List of Integers xSicKxBot 0 1,455 11-25-2022, 11:54 AM
Last Post: xSicKxBot
  [Tut] How to Filter Data from an Excel File in Python with Pandas xSicKxBot 0 1,221 10-31-2022, 05:36 AM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016