[No audio]
Good, we're continuing our series of
lectures about manipulating Pandas dataframes.
So you'll learn how to drop a
row or column out of the DataFrame in
the previous lecture. Now let's see how
we can add a column or row to a DataFrame.
So we have df7 here with
Address as index column and the series
of column labels. Now, just to clarify
something, I know this is difficult to
wrap up your mind, how you'll be using
these operations in real life? So I just
want you to know the syntax of doing
this operations, and throughout the
course, you will learn how to actually
put this into work with real life
examples. So don't worry about that.
Anyway, let's add a colon there, and to do
that, you'd want to say df and in
square brackets, you pass the name of
the new column, and let's say, Continent,
that'd be equal to, you know, now you
would have to pass a list of values that
you want to populate that column with,
let's say North America. Now, if you
execute that, you'll get an error,
name df is not defined. Actually, I
didn't mean this error. This was another
thing. You get this error. So it says
that the length of values does not match
the length of index. So the length of
index is, you know, the length of index is
df7.index, the length of your
index is 5, but you're trying to pass
their list with a length of 1. So
you've got only one element there. So
the solution here is to pass a list with
the exact number of items that you have
in your table, in your DataFrame. So we
have 5 rows there, 5, and yeah,
you could add four more items here,
North America, North America, etc, or
you can do some fancy things in there.
So you could say df7.shape then
0 times that, execute, df7, print that
out. Here, we have a new column. So if
you're confused with this shape
0 times North America, well, what I
did is, you know, df7.shape,
what you get is 5, 7, which means
you have 5 rows and 7 columns.
Now, what I want to get is the first
item of that tuple. So I get 5 by
doing shape 0. With this, I always
make sure that I'm getting the number of
rows that my DataFrame has, and then
when you multiply 5 by a list with
one element, you get a list with 5
elements. America, that's the idea,
delete that, and note that this is
actually an inplace operation. So that
will update your DataFrame, and all
that it was about adding a new column, and
how about modifying a new column.
So modifying the Continent column, you
know, that could be something like
Country, and you can also add some
streams in there, let's say plus a comma,
plus, maybe another string, North America.
[No audio]
Execute that, print all
the DataFrame, and yeah, see what we
got. So what we did here is we updated
the Continent data column by referring
to an existing column, which is
Country, and then for each value of the
Country column, we added so we
concatenate the comma string, which you
can see in here, just after USA, and we
also added another string, so North
America. Now this could also be another
column if you liked. So if you pass
here, let's say Employees, you'd get
8 instead of the first North
America, and then 15, and so on. So that's
how you update column. Now how about
adding a new row? Well, this can be a
bit tricky, but still understandable.
What you could do here, because there's
not easy methods to pass row to a
DataFrame. What you could do is you
could say df7_t, so I'm creating a
new variable, that would be equal to df7.T.
So T actually is a method
that what it does is it transposes your
DataFrame, and with transposition I mean,
you know, df7, you check
your new DataFrame. What you get is
this. So you're rows have become
columns, and your columns have become
rows. So spend a few second looking at
this, yep, and what we can do now is, you
know, we can use the same syntax, df7_t,
and then add a new column in there,
so for the name of a column, you'll
have to pass an Address, let's say My Address,
let it be equal again to a
list, and you want the list to reflect
this order. So City first, let's say, My City,
Country, My Country,
[No audio]
and 10 for Employees, and 7 for ID,
My Shop for the shop name, My State,
[No audio]
and My Continent,
execute that, and df7_t. If you look at
these now, you'll see that
[No audio]
you get a new column in your DataFrame,
and now what you do so to complete the trick is, let
me say df7 equals to df7_t.T.
[No audio]
So df7 now,
we'll have the new row added at the end. So
let me wrap this up. Again, what I did
is I transpose the original DataFrame,
df7, then I added a column there.
And now if I transpose the DataFrame
back again to its original position,
this column that I added will be
converted to a row, and that does the
trick, and similarly, you can modify a
row if you like. So in that case, you
don't point to My Address, but you'd
point to an existing column with an
address, with an existing address name.
For instance, this one, if I pass it
here, and execute that, execute that, and that,
and you'll see that the values of this row
with this address name have been updated
now, as you can see in here. Now, that
closes this lecture as well, and yeah,
I'll see you later.