{"id":112192,"date":"2020-04-29T09:47:43","date_gmt":"2020-04-29T09:47:43","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=8027"},"modified":"2020-04-29T09:47:43","modified_gmt":"2020-04-29T09:47:43","slug":"pandas-to_csv","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/04\/29\/pandas-to_csv\/","title":{"rendered":"Pandas to_csv()"},"content":{"rendered":"<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/04\/graphic-1024x576.jpg\" alt=\"Pandas to_csv()\" class=\"wp-image-8024\" width=\"512\" height=\"288\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/04\/graphic-scaled.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/04\/graphic-300x169.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/04\/graphic-768x432.jpg 768w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/figure>\n<p>You can convert a list of lists to a<a rel=\"noreferrer noopener\" href=\"https:\/\/pandas.pydata.org\/\" target=\"_blank\"> Pandas<\/a> DataFrame that provides you with powerful capabilities such as the <a rel=\"noreferrer noopener\" href=\"https:\/\/pandas.pydata.org\/pandas-docs\/stable\/reference\/api\/pandas.DataFrame.to_csv.html\" target=\"_blank\"><code>to_csv()<\/code> method<\/a>. <strong>This is the easiest method and it allows you to avoid importing yet another library<\/strong> (I use Pandas in many Python projects anyways). <\/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 = [['Alice', 'Data Scientist', 122000], ['Bob', 'Engineer', 77000], ['Ann', 'Manager', 119000]] # Method 2\nimport pandas as pd\ndf = pd.DataFrame(salary)\ndf.to_csv('file2.csv', index=False, header=False)<\/pre>\n<p>Output:<\/p>\n<pre class=\"wp-block-preformatted\"><strong><em># file2.csv<\/em><\/strong>\nAlice,Data Scientist,122000\nBob,Engineer,77000\nAnn,Manager,119000<\/pre>\n<p>You create a Pandas DataFrame&#8212;which is Python&#8217;s default representation of tabular data. Think of it as an Excel spreadsheet within your code (with rows and columns). <\/p>\n<p>The DataFrame is a very powerful data structure that allows you to perform various methods. One of those is the <code>to_csv()<\/code> method that allows you to write its contents into a CSV file.<\/p>\n<p>You set the <code>index<\/code> and <code>header<\/code> arguments of the <code>to_csv()<\/code> method to <code>False<\/code> because Pandas, per default, adds integer row and column indices 0, 1, 2, &#8230;. Again, think of them as the row and column indices in your Excel spreadsheet. You don&#8217;t want them to appear in the CSV file so you set the arguments to <code>False<\/code>. <\/p>\n<p>If you want to customize the CSV output, you&#8217;ve got a lot of special arguments to play with. Check out <a rel=\"noreferrer noopener\" href=\"https:\/\/pandas.pydata.org\/pandas-docs\/stable\/reference\/api\/pandas.DataFrame.to_csv.html\" target=\"_blank\">this <\/a>article for a comprehensive list of all arguments.<\/p>\n<p><strong>Related article<\/strong>:<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/pandas-cheat-sheets\/\" target=\"_blank\"> Pandas Cheat Sheets to Pin to Your Wall<\/a><\/p>\n<p>Feel free to play with alternative methods to <a href=\"https:\/\/blog.finxter.com\/how-to-convert-a-list-of-lists-to-a-csv-file-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">convert a list of lists to a CSV file <\/a>in our interactive code shell. Simply click the &#8220;Run&#8221; button and find the generated CSV files in the &#8220;Files&#8221; tab. <\/p>\n<p> <iframe loading=\"lazy\" src=\"https:\/\/repl.it\/@finxter\/methodscsvlistoflists?lite=true\" scrolling=\"no\" allowtransparency=\"true\" allowfullscreen=\"true\" sandbox=\"allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals\" width=\"100%\" height=\"1000px\" frameborder=\"no\"><\/iframe> <\/p>\n<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 a List of Lists to a CSV File in Python\" width=\"1400\" height=\"788\" src=\"https:\/\/www.youtube.com\/embed\/-ewNMpo_LqU?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen><\/iframe><\/div>\n<\/div>\n<\/figure>\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>You can convert a list of lists to a Pandas DataFrame that provides you with powerful capabilities such as the to_csv() method. This is the easiest method and it allows you to avoid importing yet another library (I use Pandas in many Python projects anyways). salary = [[&#8216;Alice&#8217;, &#8216;Data Scientist&#8217;, 122000], [&#8216;Bob&#8217;, &#8216;Engineer&#8217;, 77000], [&#8216;Ann&#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-112192","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\/112192","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=112192"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/112192\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=112192"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=112192"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=112192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}