Sick Gaming
[Tut] How to Convert List of Lists to a Pandas Dataframe - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.sickgaming.net/forum-83.html)
+--- Thread: [Tut] How to Convert List of Lists to a Pandas Dataframe (/thread-94769.html)



[Tut] How to Convert List of Lists to a Pandas Dataframe - xSicKxBot - 04-28-2020

How to Convert List of Lists to a Pandas Dataframe

<div><figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe title="How to Convert List of Lists to a Pandas Dataframe" width="1400" height="788" src="https://www.youtube.com/embed/pcF4rYfqs34?feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
<p><strong>Problem</strong>: You’re given a <a href="https://blog.finxter.com/python-list-of-lists/" target="_blank" rel="noreferrer noopener">list of lists</a>. Your goal is to convert it into a <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-calculate-the-column-standard-deviation-of-a-dataframe-in-python-pandas/" target="_blank">Pandas </a>Dataframe. </p>
<p><strong>Example</strong>: 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:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">salary = [['Google', 'Machine Learning Engineer', 121000], ['Google', 'Data Scientist', 109000], ['Google', 'Tech Lead', 129000], ['Facebook', 'Data Scientist', 103000]]</pre>
<p>How can you convert this into a <a rel="noreferrer noopener" href="https://pandas.pydata.org/" target="_blank">Pandas </a>Dataframe?</p>
<h2>DataFrame()</h2>
<p><strong>Solution</strong>: The straight-forward solution is to use the <code>pandas.DataFrame()</code> constructor that creates a new Dataframe object from different input types such as NumPy arrays or lists. </p>
<p>Here’s how to do it for the given example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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)</pre>
<p>This results in the following Dataframe:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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 '''</pre>
<p><strong>Try It Yourself</strong>: Run this code in our interactive Python shell by clicking the “Run” button.</p>
<p> <iframe height="700px" width="100%" src="https://repl.it/@finxter/pandaslistofliststodataframe?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p>
<h2>DataFrame.from_records()</h2>
<p>An alternative is the <code>pandas.DataFrame.from_records()</code> method that generates the same output:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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 '''
</pre>
<p><strong>Try It Yourself</strong>: Run this code in our interactive Python shell by clicking the “Run” button.</p>
<p> <iframe height="700px" width="100%" src="https://repl.it/@finxter/pandastorecord?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p>
<h2>Column Names</h2>
<p>If you want to add column names to make the output prettier, you can also pass those as a separate argument:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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 '''</pre>
<p><strong>Try It Yourself</strong>: Run this code in our interactive Python shell by clicking the “Run” button.</p>
<p> <iframe height="400px" width="100%" src="https://repl.it/@finxter/pandasconvertlistoflists?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p>
<p>If the first list of the list of lists contains the column name, use <a rel="noreferrer noopener" href="https://blog.finxter.com/introduction-to-slicing-in-python/" target="_blank">slicing </a>to separate the first list from the other lists:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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 '''</pre>
<p>Slicing is a powerful Python feature and before you can master Pandas, you need to master slicing. <a href="https://blog.finxter.com/free-book-coffee-break-python-slicing/" target="_blank" rel="noreferrer noopener">To refresh your Python slicing skills, download my ebook “Coffee Break Python Slicing” for free.</a></p>
<p><strong>Summary</strong>: To convert a list of lists into a Pandas DataFrame, use the <code>pd.DataFrame()</code> constructor and pass the list of lists as an argument. An optional columns argument can help you structure the output.</p>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>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?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?</p>
<p>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.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
</div>


https://www.sickgaming.net/blog/2020/04/27/how-to-convert-list-of-lists-to-a-pandas-dataframe/