All right, now that you know how to load
data in Python via Pandas, and you know
how to do that through using different
data sources, so csv, json, and text
files, and Excel, and now you want to
understand how and learn how to
manipulate these DataFrames, and by
manipulation, what I mean is deleting
rows and columns from your DataFrame,
and adding new rows and columns, and
also modifying existing rows and
columns. So that's what you're going to
learn throughout these lectures. But
first of all, I'd like you to understand
how DataFrames are indexed, and with
indexing, I mean, you know, we have this
DataFrame here, and this can be a big
one, because all these happens to be a
shorter one with only six rows. But if
you have big DataFrames with lots of
columns and rows, then you may want to
extract information out of the DataFrame,
and to extract information, you
need to have like a coordinate system,
only that DataFrame like an embedded
coordinate system. So that if you want
to access, let's say, 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 know how DataFrames
are indexed and how you can slice them.
So let's try to extract that portion of
the DataFrame. There might be different
ways to access that portion of the DataFrame.
The first way is to use a label-based
indexing. The other way is to use
position-based indexing. So your DataFrame
has column labels and index
labels. So now you can use labels from
your index column and labels from
your header, your column names to access
portions of your DataFrame. With label
indexing, you want to use loc in there
so the loc 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 and not
strings, so you will have to pass, you
know 735 Dolores St, and then a
range, so with a column there, 332 Hill St,
and then from Country
[No audio]
to ID, execute
that. I know this is our portion. So when
you use labels, you're including the
first label that you pass there and the
last one as well. So everything between
those, and like here, Country and
Employees is included as well, but ID
also, and of course, similarly, almost
similarly, you can access, you know,
single cells from your DataFrame, 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 the USAs, then you just
pass everything there, and you get
everything here, which of course, if you
want, you can convert it to list.
[No audio]
So a simple list using the Python built-in
function, which is list and that's about
label-based indexing. Now, this is not
the common way to access to extract data
from a DataFrame. More common could be
to access a data based on indexing, not
based on labels. So 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.
[No audio]
Actually, let me print all the DataFrame
here so that you
[No audio]
can refer to that. So let me access from
Dolores to 23rd street, and that
would be 1 to 3, I believe, yep, and also
from Country to ID. So again 1 to
[No audio]
3,
[No audio]
and here, you can see the difference now,
you know, the ID wasn't included there
and neither was 23rd Street, because this
is as, as you do with lists, this is upper
bound exclusive. So with Python list
3 is not included in the
slice, but with labels, that the last
item or 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.
[No audio]
So that would be a row
with index 3, which is this one, but
only four columns, Country,
Employees, and ID. So USA, 10, and 4.
All right, that is position-based
indexing, and yeah, that's what I wanted
to teach you about DataFrame indexing
and slicing, and I'll talk to you in the next lecture.