Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Writing CSVs With Pandas

In this lesson, you’ll cover how to use pandas to write data to CSV files. Here is a code sample that demonstrates how to add a new employee to a CSV file from the previous lesson:

data_frame.loc['Cookie Cat'] = ['2016-07-04', 20000.0, 0]


Sample CSV Files (.zip)

2.3 KB

00:00 Once you have the data from a CSV in pandas, you can do all sorts of operations to it as needed. When you want to get that data out of pandas, it can be helpful to put it back into a CSV.

00:11 Let’s quickly add a row for a new employee named Cookie Cat.

00:19 This is more for just showing that we can change the DataFrame—we’re not going to get into how to work with DataFrames too much in this video. Let’s see, I found her on the 4th of July,

00:33 she gets about 20,000 treats a year, and she doesn’t have any sick days left because she’s pretty lazy. All right, let’s print this DataFrame out after we add the row.

00:47 And let’s just see—python All right. So if you notice, we have the new row here. We don’t have the date in the same format—now this is just a date, it’s not a date and a time—but we won’t worry about that too much.

01:01 Everything else looks okay. So what you can actually do is just replace this print statement with a .to_csv(), and just pass in a new filename. So we’ll just say 'hrdata_modified.csv'. Go ahead and run that.

01:19 And we have a new file here, so I’ll open this up—and there you go! Look, you’ve added a new row. You can see that these hired dates are different than they were when we first read them in.

01:33 So, pandas is only saving the string representation of what it has for its own Timestamp object, so depending on where the CSV needs to go after this, you may need to take that into account.

01:46 But that’s enough for now. In these videos, you learned how to read and write CSVs with Python using two separate libraries, and even covered ways to handle nonstandard data.

01:57 If you find yourself working with structured data often, I highly recommend looking into pandas, because it’s a great library. Now it’s time to start using CSVs in your own applications.

02:07 Thanks for watching, and have fun!

SamR on June 17, 2019

Very helpful information. Thank you!

Sandhya on Feb. 13, 2020

Very helpful. Thanks.

Indran Naidoo on March 12, 2020

Thank you for a succinct walkthrough.

Joel on April 15, 2020

good job, must look deeper into pandas regarding not “hard-coding” the data in the python code

sroux53 on May 13, 2020

Excellent !

Kasidis Satangmongkol on June 10, 2020

Thank you very much! Great course, love your style of teaching, very easy to follow ;)

jamesbrown68 on July 27, 2020

Nice beginner course. Thank you.

tcconde on Aug. 15, 2020

My number 1 takeaway is that I did not learn anything. All he really said was “Use Pandas”, beyond that, not much happening.

patientwriter on Nov. 18, 2020

Uhh… I was a little surprised that your download was data, and not code. We can get data anywhere. The point of the class was the code, wasn’t it?

aniketbarphe on Nov. 28, 2021

Thank You!

Aryan Verma on March 23, 2022

Great tutorial, which software were you using while teaching us?

Jesus Espinoza on Oct. 22, 2022

Thank you!!! Really usefull tutorial. A great beginning for every ones who wants to start in Data Sciences with Python and Pandas.

pedroifgonzalez on Feb. 1, 2023

Great course! I already knew about the csv built-in module, but was great to know more in depth about it, using the quotechar, escapechar and quoting parameters. And of course mentioning Pandas could not be missing.

Become a Member to join the conversation.