Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

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:

Python
data_frame.loc['Cookie Cat'] = ['2016-07-04', 20000.0, 0]
data_frame.to_csv('hrdata.csv')
Download

Sample CSV Files (.zip)

2.3 KB

Take the Quiz: Test your knowledge with our interactive “Reading and Writing CSV Files in Python” quiz. You’ll receive a score upon completion to help you track your learning progress:


Interactive Quiz

Reading and Writing CSV Files in Python

This quiz will check your understanding of what a CSV file is and the different ways to read and write to them in Python.

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 pandas_csv.py. 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!

Avatar image for SamR

SamR on June 17, 2019

Very helpful information. Thank you!

Avatar image for Sandhya

Sandhya on Feb. 13, 2020

Very helpful. Thanks.

Avatar image for Indran Naidoo

Indran Naidoo on March 12, 2020

Thank you for a succinct walkthrough.

Avatar image for Joel

Joel on April 15, 2020

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

Avatar image for sroux53

sroux53 on May 13, 2020

Excellent !

Avatar image for Kasidis Satangmongkol

Kasidis Satangmongkol on June 10, 2020

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

Avatar image for jamesbrown68

jamesbrown68 on July 27, 2020

Nice beginner course. Thank you.

Avatar image for tcconde

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.

Avatar image for patientwriter

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?

Avatar image for aniketbarphe

aniketbarphe on Nov. 28, 2021

Thank You!

Avatar image for Aryan Verma

Aryan Verma on March 23, 2022

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

Avatar image for Jesus Espinoza

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.

Avatar image for pedroifgonzalez

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.

Avatar image for toigopaul

toigopaul on Jan. 4, 2024

00:47 And let’s just see—python pandas_csv.py. 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.

This is not a date. This is text. You need to import datetime and change the Cookie Cat line to:

df.loc['Cookie Cat']=[datetime.datetime.strptime('2016-07-04','%Y-%m-%d'),2000,0]

Doing this little change not only makes Hired for Cookie Cat a date, the 00:00:00 no longer appears in any record.

Become a Member to join the conversation.