- [Instructor] The Pandas Library has built-in support
for reading and writing a variety of data file types.
In this example, we'll see how to read Excel and CSV files.
So here in my pandas_read.py file,
you can see I've already imported pandas as the pd alias,
and this is pretty common among Pandas users,
you'll probably see this code quite a bit.
That makes it easier to refer to the library.
So for our first example,
let's read the content of our inventory.csv file,
which you can see is located right here
in the same directory.
So to do this, you just need to use
the built-in read_csv function that Pandas provides.
That's going to build and return a data frame object.
So I'm going to create a variable called df
for the data frame.
And on pandas, I'm going to call read_csv
and specify the name of Inventory.csv.
And then I'm just going to print out the data frame.
So the result of this function
will be a filled out data frame
that contains the CSV file contents.
So let's go ahead and run what we have in the terminal.
And sure enough, you can see that when we run the code,
the output is a nicely formatted table of data,
which is the contents of the CSV file.
So Pandas has created columns
with the same names as the first row in the file,
and has also created rows
with numeric indexes starting at 0.
And we can see that all 50 rows of data are present.
So I mean, that's pretty simple, right?
But we can also customize the process.
So, for example, I can specify
that I only want to read a certain number of rows,
and I can specify that I want to skip a number of rows
if I'm working with a large file.
So let's go back to the code,
and let's just read a portion of the file
so I'm going to copy this example and paste it here.
I'm going to specify that I'm going to skip rows
by using the skiprows parameter.
I'm going to skip the first 15 rows,
and then I only want to read 15 rows
so I'm going to use the nrows parameter
to specify that I want to read 15 rows of data.
And let's comment out the previous example.
Now, but the problem here is that if I do this,
I'm going to skip the header row,
and that's going to cause an issue.
So watch as I run this.
Right, we can see that the headers are all messed up, right?
It doesn't know what the headers are
because we skipped over them.
So to fix that, I can specify a function
that returns true if a line should be skipped
and false otherwise.
And I'm going to use an inline lambda function
to achieve this.
So let's go ahead back here.
And what I'm going to do is I'm going to write skiprows=
and I'm going to write lambda x.
So if x is greater than or equal to 1
and x is less than 15, right?
Then that's going to return true,
otherwise it's going to return false.
So this is a big Boolean operation right here.
So it's going to skip the first row,
but not the 0 width row,
which has all of my headers in it.
So let's save this and let's run it again.
And now you can see that the data is correct,
the headers are all correct,
and I've skipped 15 rows,
and I've only read in 15 rows of data.
All right, let's try some of this with an Excel file
and let's comment out the previous example.
So I'm going to read the financial sample file
that we've been using in the course,
which, as we saw earlier, is pretty large, right?
That's 700 rows of data.
So to read the Excel file,
similar to using read_csv,
Pandas provides a read_excel function.
So I'm going to go ahead and call that,
so that's going to give me a data frame,
I'm going to call read_excel.
And I want to read the FinancialSample.xlsx file.
I can also specify some nice things.
I want to specify that I'm going to use only certain columns,
and I'm interested in columns A:E and column H,
and I'm only interested in 15 rows.
All right, so let's print out the data frame
when we do that,
and let's also print out
the data frame types,
so we'll see what types of data are stored in the table.
So let's run this.
And oh, whoops,
looks like I've got
this wrong property.
I believe it's actually dtypes.
There we go.
Let's try this again.
Yep, there we go.
All right, so you can see that when I run this,
we have the columns, right?
So here's all the columns,
we've got 15 rows of data,
and you can also see that I'm only getting
columns A:E and column H.
And we have the data types.
So the text columns are objects,
and these columns are float64s.
We can also get information about an Excel file
by using the ExcelFile class
that is part of the Pandas module.
So let's go ahead and comment this out.
So in this case, I'm going to get a file result,
and on the Pandas object
I'm going to use the ExcelFile class,
and I'm going to pass in the FinancialSample file.
So this will give me a reference to the Excel file,
and then I could do things
like print out some information about the file.
So I can examine the sheet names, for example.
So the sheet names are:,
and I'm going to write file.sheet_names.
All right, let's go ahead and try that,
and let's run.
And we can see, sure enough,
we have these SalesData and Sheet2 sheets.
So, of course, once we've loaded the data into a data frame,
we're going to want to perform some operations on it,
and we will see how to do that later in the chapter.