Alright now that's you know how to get data in Python via Pandas and you know how to do that through
using different data sources as is so CSV, JSON, text files an Excel. And you want to understand
how and learn how to manipulate these data frames and by manipulation what I mean is deleting in
rows and columns from your data frame and adding new rows and columns and also modifying existing rows
and columns.
So that's what youre gonna learn throughout these lectures.
But first of all I'd like you to understand how data frames are indexed. With indexing
i mean you know we have this data frame here and this can be a big one.
So this happens to be a short one with only six rows but if you have big data frames with lots
of columns and rows then you may want to extract information out of the data frame and to extract
information you need to have like a coordinate system
in that data frame like an embedded coordinate system so that if you want to access let's say
from here and 15 to and then maybe this other portion.
So these two rows here, this portion here you want to know how to do that.
So that's what you're going to learn now how did our frames are indexed how you can slice them.
So let's try to extract that portion of the data frame.
Let me create some empty lines here.
I don't want to confuse you with that data frame.
So we're working on this one.
And then yeah, df7 and what you want to apply now there might be different ways to access that portion
of the data frame.
The first way is to use label based indexing.
The other way is to use position based indexing.
So your data frame has column labels and index labels.
So these 1 0 1 2 3 4 5 are index labels.
These are column labels.
Now these, the index label in our case it's a bit confusing because it's normally it can be text
such as let's say in our data frame the address is a unique column which you can use as an identifier.
So if you want you can set that as an index. Address, execute. And so you see that this is in bold.
So the first column is in bold meaning that this is the index column of your data frame.
So now you can use labels from your index column and labels from your header
your column names to access portions of your data frame.
Just be careful though be because with this operation that we just do here you know if you print
df7 now Df7. You'll see that you see you have the old index column in there.
So 0 1 2 3 5.
The reason to that is this set index method as many other methods in Pandas is not an in-place
operator.
Meaning that all this will do it will speed out an updated data frame, but it will not store that data
frame in your variable.
So the variable, the data frame remains the same.
It just prints out a new data frame on the fly just for printing purposes.
So what you want to do to store that is you create a variable which can be the same variable as this if you
don't If you just want to update your variable.
But it can also be another variable like df8.
I'll just want to update existing variable like that. That will not print out any output.
So you want to do that manually like this.
So now when you print the df7 you see that
df7 is updated with this address column as an index of the data frame.
So be careful with in-place operations.
You know you can see that df.set_index question mark to ask for help.
Sorry, it should be df7.set_index. And you can see that you have this inplace parameter there.
That is sat to False by default.
So which means you can also pass inplace after this and set it to True you will update the existing
data frame without the need to save the data frame to a new variable.
But I would recommend that you just get used to creating new variables when you apply methods
to your existing variables, existing data frames. And it should be good to go. Delete that and let's
get back to our original purpose now which was to access, to slice this portion of the free. I would first
do it using a label indexing. With the label indexing you want to use a lock in there. So the lock method.
And then you pass square brackets in there and then that gets two elements and the first element could
be a range of the index column.
So we're talking about labels, about strings so you have to pass you know 735 Dolores St
and than a range
so with a column there 332 Heel St. And from country to ID. Execute that.
And yeah this is our portion.
So when you use labels you're including the first label that your pass there and the last one as well.
So everything between those like here country and employees is included as well but ID also and of course
similarly almost similarly you can access you know new single cells from your data frame just like that.
So the intersection between this index label and this column name is USA which would be this one here.
If you want all of the USAs then you just pass everything there and you get everything here which
of course if you want you can convert it to a list.
So a simple list using the Python built in function which just List. And thats about label based indexing.
Now this is not the common way to access to extract data from the data frame. More common could be
to access the data base on indexing not based on labels.
Su to do that you do df7 and instead of loc you do iloc.
That again expects two items.
So the first would be the range of your indexes.
Actually let me print all the data frame here so that you can refer to that.
So let's me access from Dolores to 23rd Street.
That would be 1 to 3 I believe. Yeah.
And also from country to ID.
So again 1 two 3.
And yeah, you can see the difference now.
You know the ID wasn't included there and either was 23rd Street because this is as you do
with Lists this is Upper Bound exclusive.
So with Python List 3 is not included in the slice, but with labels the last item of the
range was included in the slice.
So in this case you want to pass 4 there and 4 there.
And that's how you get your portion.
And of course similarly you can do things like that.
So you get all the rows or only one of them.
So that would be a row with index 3 which is this one but only for columns Country, Employees
and ID.
So USA 10 and 4.
All right.
That is position based indexing.
Now we have one more very useful kind of indexing and that is combined indexing.
So in certain scenarios you might need to access a portion of rows without having to pass names for
the rows.
So you want to access rows based on indexing, based on their position but on the other hand you don't
want to access columns based on positions you want to access like sort of columns
which names you know about.
So you don't want to count the column and here comes in handy
the IX Method.
So that allows you to enter a combination of indexes and labels.
So that will return you the row with index 3 and the column with label name so we get Bens Shop
and you cannot do this neither with iloc nor with loc.
That's why IX exists there.
And of course you can put two positions if you like so name would be 0 1 2 3 4 you get the same outputs
.
Bens Shop.
Which brings us to the point that if you want you can always use IX.
Forget about iloc and loc but IX is not recommended
over iloc and loc.
So these are more explicit.
They will avoid errors some time.
So when you do position based indexing.
So I also suggest you use iloc and loc whenever you can and then switch to IX when you want to do
these kinds of combinations between labels and positions.
And yeah, that's what I wanted to teach you about data frame indexing and slicing.
And I'll talk to you in the next lecture where you'll be learning how to delete columns and rows from
the data
frame
See you there.