Sick Gaming
[Tut] Pandas to_csv() - 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] Pandas to_csv() (/thread-94802.html)



[Tut] Pandas to_csv() - xSicKxBot - 04-30-2020

Pandas to_csv()

<div><figure class="wp-block-image size-large is-resized"><img 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="(max-width: 512px) 100vw, 512px" /></figure>
<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>
<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
import pandas as pd
df = pd.DataFrame(salary)
df.to_csv('file2.csv', index=False, header=False)</pre>
<p>Output:</p>
<pre class="wp-block-preformatted"><strong><em># file2.csv</em></strong>
Alice,Data Scientist,122000
Bob,Engineer,77000
Ann,Manager,119000</pre>
<p>You create a Pandas DataFrame—which is Python’s default representation of tabular data. Think of it as an Excel spreadsheet within your code (with rows and columns). </p>
<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>
<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, …. Again, think of them as the row and column indices in your Excel spreadsheet. You don’t want them to appear in the CSV file so you set the arguments to <code>False</code>. </p>
<p>If you want to customize the CSV output, you’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>
<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>
<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 “Run” button and find the generated CSV files in the “Files” tab. </p>
<p> <iframe 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>
<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 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>
</div>
</figure>
<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/29/pandas-to_csv/