- In my previous two Intro to Data Science presentations,
I focused on the Pandas library for working
with one-dimensional and two-dimensional data
as series and data frames respectively.
Here I want to continue that discussion,
bringing in Pandas with CSV files.
So, as you can see,
I've already imported the Pandas library,
and it turns out that Pandas makes it super easy
to read an entire CSV file
into a Python program,
and also to create CSV files from data frames, as well.
So let's paste in a statement here.
We're going to create a data frame
by calling the Pandas library's read CSV function
the name of the file that we want to read
is provided as an argument.
And I'm assuming this file is in the same folder
from where I launched IPython,
and it is in this case.
And the second argument is the column names
for the new data frame that we're going to create.
Now, if you do not provide this second argument,
the read CSV function is going to assume
that the very first row in the comma separated values file
contains the names of the columns already.
So, in our case you may recall,
that we did not write out a line of text indicating
what the column names were in the accounts.csv file.
So if we do not include this, then it would accidentally
use the very first record of information
as the column names in our Pandas data frame.
So let's go ahead and create that data frame,
and of course we can evaluate it to see what it looks like.
And you'll notice that we get the account, name,
and balance columns as specified
by the second argument up here.
It provides indices for the rows,
which are just zero through four
by default because we didn't
give it custom index values.
And you can in fact see the same data
that we've worked with previously in text files
and in the previous couple of videos
with our presentation of the CSV module.
Now, in addition to being able to create
data frames really easily
from a properly formatted CSV file,
you also have the ability to output a CSV file
from a data frame.
So let's assume you've already created a data frame.
You've done all sorts of data wrangling and munging
to get the data into the form that you want,
and now what you want to do is create
a comma separated value file that stores
that information in a persistent manner.
So I'm going to go ahead and copy and paste
a statement in here that's going to demonstrate
the to CSV function method of a data frame.
The first argument will be the filename that you specify
for the comma separated value file.
Of course, we did not provide any path information,
so this will simply be placed in the same folder
from which I launched IPython.
And the second argument, index=False simply means
that we should not output the zero, one, two, three, four
index numbers as part of the comma separated value file.
So what's actually going to get written is
everything that you see in these three columns:
the account column, the name column,
and the balance column.
And the first row of the file will be
the string's account name and balance
separated by commas, so that it includes in the file
what the purpose of each value is in a given record
of information.
So I'll go ahead and do that,
and to show you that it did write the file,
and also that it wrote out
the line of column names.
Let's display the contents of the file here in IPython,
and notice that we have this one line of text,
which is common by the way in most of the data sets
that you have access to online.
Typically the first line is going to have
the column names to give you a sense
of what the purpose of the columns are
in a given dataset file.
So as you can see, it's really easy to load a CSV file
into a data frame for processing,
and also once you've processed your data in the data frame,
to save that information out to a CSV file as well.