- [Instructor] Of course, we don't want to just read data
with pandas, we want to be able to write data as well.
So for this example,
we're going to open up our pandas_write.py file.
And you can see that I've defined an array of objects
that I would like to write out to a CSV file.
And you can see that the data has the same format
as our inventory.CSV sample file.
It's just a lot smaller
and it's in the format
of dictionaries rather than an array of lists.
So what I need to do first is create a data frame
to represent the data.
So to do that I'm going to create a data frame variable,
and on pandas, I'm simply going to use the data frame
constructor and pass in my array of objects.
Then I need to save the data as CSV format,
and I can do that by using the appropriately named
to CSV file.
So I'll call DF and I'll call to CSV
and I'm going to name it output.CSV.
All right, so let's go ahead and run that.
And when I do, you can see that output.CSV gets created
and we can go ahead and look at that in our text editor
and you can see that pandas added index values
to the beginning of each row except for the header row.
Now in some cases that might be what you want,
but if you don't want the indexes to be added for you,
then you can specify that when you call the function.
So let's go ahead and update our code
and I'm going to specify index equals false.
All right, so let's save, let's run that again.
All right, and then let's click on our file.
And now you can see that the indexes are no longer present.
You've probably realized by now
that we can convert a CSV file directly to an Excel file
with just two lines of code.
So let's go ahead and try that.
So in this case I'm going to write DF equals
and I'm going to call read CSV on inventory.CSV
and then I'm going to call DF.
And in this case I'm going to call the corresponding
to Excel function.
I'm going to name it inventory.XLSX.
I'm going to specify the sheet name to be inventory.
And once again, I'm going to specify
that I don't want the index values.
All right, and let's comment out the previous example.
Let's save and let's run this.
And sure enough we can see that inventory.XLSX
has been created.
Let's go ahead and look at that in Excel
and sure enough, yep, data looks all right to me.
There's 50 rows, there's the headers, everything looks good.
It's important to note that the right functions will
overwrite any existing file that already has that name.
So if you want to add new data to an existing Excel file,
you need to do it by using the Excel writer class
that Pandas provides.
So let's go back to our code
and let's add a new worksheet to the one
that we just created.
And this worksheet will contain the same data,
but with the indexes included.
So first we have to open the file in append mode.
So I'm going to write with pd.ExcelWriter,
and I'm going to open up the inventory.XLSX file
and we have to open it in append mode.
So mode is going to be A, all right,
and I need to do one other thing.
I need to specify that the underlying engine to use is going
to be OpenPyXL.
So pandas is going to detect
which libraries you have installed
for working with Excel files.
And it's not going to choose OpenPyXL by default,
but other libraries like XLSX Writer for example,
don't support appending data to existing workbooks.
So we need to explicitly tell pandas
to use the OpenPyXL library.
So I'm going to do that by specifying engine equals,
I'm going to write OpenPyXL.
All right, so now I've got my completed statement here
and I'm going to open that as the XLW variable.
And now I just need to write the new data.
So the data frame, I'm going to call to Excel
and I'm going to pass in my Excel writer object
that I just created right here.
And I'm going to specify the sheet name
is going to be inventory indexes,
and I want the index label
to be index.
All right, so let's comment out the previous example
because we've already created the file,
so we don't need to do that again.
So you can see that the file exists right here.
All right, and oh, actually I need to
have the data frame though.
All right, so let's go ahead and run this one more time.
All right, there we go.
Okay, it looks like it's been updated.
So let's open up the result in Excel.
And sure enough, now you can see we have two sheets.
We have the original sheet, which is untouched,
and now we have the new inventory indexes sheet.
And you can see that in this case,
pandas has added the numeric indexes for me.
So using pandas, it's very easy to both read
and write data in multiple formats like CSV and Excel.