{"id":112097,"date":"2020-04-27T08:28:24","date_gmt":"2020-04-27T08:28:24","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=7942"},"modified":"2020-04-27T08:28:24","modified_gmt":"2020-04-27T08:28:24","slug":"how-to-convert-list-of-lists-to-a-pandas-dataframe","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/04\/27\/how-to-convert-list-of-lists-to-a-pandas-dataframe\/","title":{"rendered":"How to Convert List of Lists to a Pandas Dataframe"},"content":{"rendered":"<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<div class=\"ast-oembed-container\"><iframe loading=\"lazy\" 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>\n<\/div>\n<\/figure>\n<p><strong>Problem<\/strong>: You&#8217;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>\n<p><strong>Example<\/strong>: Say, you want to compare salary data of different companies and job descriptions. You&#8217;ve obtained the following salary data set as a list of list:<\/p>\n<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>\n<p>How can you convert this into a <a rel=\"noreferrer noopener\" href=\"https:\/\/pandas.pydata.org\/\" target=\"_blank\">Pandas <\/a>Dataframe?<\/p>\n<h2>DataFrame()<\/h2>\n<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>\n<p>Here&#8217;s how to do it for the given example:<\/p>\n<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>\n<p>This results in the following Dataframe:<\/p>\n<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\n0 Google Machine Learning Engineer 121000\n1 Google Data Scientist 109000\n2 Google Tech Lead 129000\n3 Facebook Data Scientist 103000 '''<\/pre>\n<p><strong>Try It Yourself<\/strong>: Run this code in our interactive Python shell by clicking the &#8220;Run&#8221; button.<\/p>\n<p> <iframe loading=\"lazy\" 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>\n<h2>DataFrame.from_records()<\/h2>\n<p>An alternative is the <code>pandas.DataFrame.from_records()<\/code> method that generates the same output:<\/p>\n<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)\nprint(df) ''' 0 1 2\n0 Google Machine Learning Engineer 121000\n1 Google Data Scientist 109000\n2 Google Tech Lead 129000\n3 Facebook Data Scientist 103000 '''\n<\/pre>\n<p><strong>Try It Yourself<\/strong>: Run this code in our interactive Python shell by clicking the &#8220;Run&#8221; button.<\/p>\n<p> <iframe loading=\"lazy\" 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>\n<h2>Column Names<\/h2>\n<p>If you want to add column names to make the output prettier, you can also pass those as a separate argument:<\/p>\n<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($)'])\nprint(df) ''' Company Job Salary($)\n0 Google Machine Learning Engineer 121000\n1 Google Data Scientist 109000\n2 Google Tech Lead 129000\n3 Facebook Data Scientist 103000 '''<\/pre>\n<p><strong>Try It Yourself<\/strong>: Run this code in our interactive Python shell by clicking the &#8220;Run&#8221; button.<\/p>\n<p> <iframe loading=\"lazy\" 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>\n<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>\n<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])\nprint(df) ''' Company Job Salary($)\n0 Google Machine Learning Engineer 121000\n1 Google Data Scientist 109000\n2 Google Tech Lead 129000\n3 Facebook Data Scientist 103000 '''<\/pre>\n<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 &#8220;Coffee Break Python Slicing&#8221; for free.<\/a><\/p>\n<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>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<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>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem: You&#8217;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&#8217;ve obtained the following salary data set as a list of list: salary = [[&#8216;Google&#8217;, &#8216;Machine Learning Engineer&#8217;, 121000], [&#8216;Google&#8217;, &#8216;Data Scientist&#8217;, 109000], [&#8216;Google&#8217;, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-112097","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/112097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=112097"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/112097\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=112097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=112097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=112097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}