Good we're continuing our series of lectures about manipulating Pandas data frames.
So you learned how to drop a row or column out of a data frame in the previous lecture.
Now let's see how we can add a column or row to a data frame.
So we have df7 here with Address as index column and this series of column labels.
Now just to clarify something.
I know this is difficult to wrap up your mind.
How will you be using these operations in real life?
So I just want you to know with the syntax of doing these operations and throughout the course you learn
how to actually put these into work with real life examples.
So don't worry about that.
And you know let's add a column there.
To that you'd want to say df and in square brackets you pass the name of the new column. Let's say Continent.
That would 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. Named df is not defined.
Actually I didn't mean this error.
This was another thing. You'll 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 the index is df7.index.
The length of your index is five.
But you're trying to pass there a 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 data frame.
So we have five rows there five.
And you know you could add four or more items here in North America. North America etc.
Or you can do some fancy things in there. So you could say df7.shape and then zero times that. Execute.
Df7.
Print that out.
Here we have a new column.
So if you're confused with this shape.
Zero times North America.
Well what I did is you know df7.shape. What you get is 5,7 which means you have five rows and seven
columns.
Now what I want to get is the first item of that's tuple.
So I get the five by doing shape zero. With this I always make sure that I'm getting the number of the rows
that my data frame has.
And then when you multiply five by a list with one element you get a list with five elements.
America. Thats the idea.
Delete that and note this is actually an inplace operation so that will update your data frame and that
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 strings in there. Let's say plus
comma plus maybe another string.
North America.
Execute that. Prints out the data frame and see what we got.
So what we did here is we updated the Continent column by referring to an existing column which was
Country.
And then for each value of the country column we added so we concatenated the comma string which you can
see in here just after USA and we also added another string.
So North America. And 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 a column.
How about adding a new row?
Well this can be a bit tricky but still understandable.
What you could do here because there is not an easy method to pass a row to a data frame
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 is actually is a method that what it does is it transposes your data frame. With transposition
I mean you know df7, you check your new data frame and what you get is this.
So your rows have become columns and your columns have become rows.
So spend a few seconds looking at this you know. 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 will have
to pass an address
Let's say My address.
That would equal again to the list and you want the list to reflect this order. So City First.
Let's see My city. Country, My country.
And 10 for employees and 7 for ID. My shop for the shop name. My state.
And My continent. Execute thati. Df7_t.
If you look at this now you'll see that you get a new column in your data frame and now what
you do?
So to complete the trick is let me say df7=df_t.T..
So df7 now will have the new row added at the end.
So let me wrap this up.
Again what I did is I transposed the original data frame df7 and than I added a column there.
And if I transpose the data frame back again to its original position this column that I added will
be converted to a row and that does the trick.
Similarly you can modify or row if you like.
So in that case you don't point to my address but you'd pointed 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 and that closes this lecture as well.
And yeah, I'll see you later.