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



Forum Jump:


Users browsing this thread:
1 Guest(s)

[-]
Discord

[-]
Active Threads
Microsoft - 4 new Flipgrid features ampl...
Last Post: xSicKxBot
Today 04:57 AM
» Replies: 0
» Views: 0
iOS 11.4 - 11.4.1 Jailbreak (Electra Jai...
Last Post: WilliamAsymn
Yesterday 11:21 PM
» Replies: 1
» Views: 804
(Indie Deal) RE8, Frontier, Slitherine, ...
Last Post: xSicKxBot
Yesterday 08:49 PM
» Replies: 0
» Views: 4
Microsoft - New hardware from Designed f...
Last Post: xSicKxBot
Yesterday 08:49 PM
» Replies: 0
» Views: 2
News - Guide: 30 Upcoming Nintendo Switc...
Last Post: xSicKxBot
Yesterday 08:49 PM
» Replies: 0
» Views: 3
News - Fortnite Skin Leak Roundup: Spide...
Last Post: xSicKxBot
Yesterday 08:48 PM
» Replies: 0
» Views: 4
kernel-debug-modules-4.18.0-305.25.1.el8...
Last Post: WilliamAsymn
Yesterday 04:14 PM
» Replies: 1
» Views: 34
Microsoft - Full-time food and travel vl...
Last Post: xSicKxBot
Yesterday 02:35 PM
» Replies: 0
» Views: 3
News - No Gravity Games Is Giving Away 1...
Last Post: xSicKxBot
Yesterday 02:34 PM
» Replies: 0
» Views: 4
News - Steam Autumn Sale Is Live Now: Ch...
Last Post: xSicKxBot
Yesterday 02:34 PM
» Replies: 0
» Views: 4

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com



Discord Server © SickGaming.net 2012-2021