- [Instructor] Data frames make it easy to perform
all kinds of manipulation on the data that they contain.
So for this example,
I'm going to focus on several of the more common operations.
Let's open up pandas_manipulate.
And you can see in my example file,
I already have some code that reads the Inventory.csv file
into a data frame.
So for the first example, let's create a new column of data.
We did this earlier in the course manually,
but now we're going to accomplish
the same thing using pandas.
We're going to add a column named margin
that represents the difference between the consumer price
and the wholesale price for each product.
So all I need to do is name the new column
and use a formula to calculate the difference
between the other two columns.
So watch how easy this is.
I'm just simply going to on the data frame make a new column.
I'm going to call that margin.
And I'm going to set that equal to the difference
between the consumer price.
And I'm going to subtract off
the column for the wholesale price.
And that's all there is to it.
This will create a new column named margin
and calculate the difference for each row in the data set.
And let's also print out the shape of the data
along with the first five rows.
All right, so let's go ahead and save our code
and let's run this.
All right, and you can see that now we have a new column.
So the shape has changed from being 50 columns,
or, I'm sorry, 50 rows and five columns
to 50 rows and six columns.
And we can see that the new margin column
is here in the result.
So we've modified the shape of our data structure.
We can also modify the contents
of a column directly in place.
Now, there's a variety of ways to do this,
and the method I'm going to use
is by using the apply function.
So the apply function allows you to apply another function
to each cell in a particular column.
So suppose I wanted to modify the cells
in the category column.
Let's create some code to do that.
And I'm going to comment out the previous example.
So on the data frame,
I'm going to specify I want to work on the category column.
And what I'm going to do is assign that to the result
of the apply function.
Now, the argument to the apply function is any function
I want to call for each cell in the column.
So let's just make this simple.
Let's convert each string
in the category column to uppercase.
To do that, I'm going to just supply
an inline lambda function for that.
And I'm going to call x.upper.
So the argument to my lambda function,
there's going to be one argument.
It's going to be the string,
which is the value of each cell in the column.
And since it's a string,
I can use the python upper function
to convert it to uppercase.
And let's also print the data frame
when we're done with that.
All right, so let's go ahead and save.
And now let's run.
And here you can see in the result in my category column,
all of the strings have now been converted to uppercase.
If you want to rename the columns on the data frame,
that's also pretty easy.
We can use the rename function for this.
And again, there are a few ways to call this function,
depending on your use case,
but I'll demonstrate how to rename specific columns.
So let's assume we want to rename wholesale price
and consumer price to just wholesale and consumer.
We can use the rename function
and pass a dictionary of old
and new names using the columns parameter.
So on the data frame,
I'm going to call the rename function,
and I'm going to specify that I'm renaming columns.
And inside this object,
I'm going to give key value pairs
and the key is going to be the column I want to be renamed
paired with what the new name is.
So let's do that for wholesale price,
and let's also do consumer price.
We will rename that to just consumer.
And then I have to specify that I want this
to happen in place.
So I'm going to set the in place argument to be true.
And then once that's done,
we'll just call the head function
to print out the first five lines.
Let me comment out the previous example.
All right, so let's go ahead and save this and let's run it.
And oh, whoop, I have to actually print the head,
not just call it.
All right, let's try that again.
All right, and so now we can see that the wholesale price
and consumer price columns have been properly renamed.
All right, one more thing.
Let's finally look at how to delete a column of data.
So to delete a column of data,
we use the drop function
with the name of the column that we no longer want.
So let's comment on this.
So on the data frame, I'm going to use the drop function.
So let's drop the margin column that we've already made.
And in order to do that,
I'll have to re-enable the code
that creates the margin column.
All right, and in the drop function,
I have to specify, once again,
I want this to happen in place.
I don't want to create a new data frame.
So I'm going to set in place equals to true.
And then I have to specify what's called the axis.
The axis I'm going to set to the value of one.
This basically means to operate on the columns,
not the rows,
because otherwise the pandas library doesn't know
whether this is a row index label
or a column index label.
So by specifying the axis parameter with a value of one,
I'm saying, hey, this is a column,
so go look for the column named margin and drop that.
And then after we've done that,
let's go ahead and print out the head one more time.
So let's re-enable the code that shows the margin there,
and then we'll print it again after it's gone.
So let's go ahead and save this and let's try it.
All right, so in the first example,
we can see that we created the margin column properly
and then we can see that it's been dropped.
All right, so this should give you some sense
of how powerful
and flexible data frames are for working with data content.
And I would suggest maybe spending some time here
referring to the docs
and trying out some of your own experiments
before moving on.