Hi, welcome back. In this video, you are
going to learn how to filter data from
your DataFrame. By that I mean
extracting particular groups of data
based on different types of conditions.
For example, let's say in our DataFrame,
we have different courses, even
though here you only see the Python
Mega Course. Because we are only seeing the
first five rows, we have other courses
with ratings. So if you want it, for
example, to know the average rating for
a particular course only, then you'd
have to filter the data by applying some
conditions. So filtering allows you to do
analysis on a particular group of your data.
Let's do that. First, I'm going to assign a number
to each of the main headings. So that was an overview,
the second lecture was selecting. So
that's number 2. This time we
[No audio]
are at number 3, filtering data based on
conditions. Right, so let's apply one
condition first. So I'm going to create
a Markdown cell and say, One condition.
Let's apply a condition which would give
us the DataFrame where the ratings are
greater than 4. To do that, you'd
have to follow this syntax. Now first,
make sure that you have your data
variable in the namespace by executing
this first cell. So Ctrl Enter to
execute that cell. Now we are sure that
data is in the namespace. So this
is the syntax data square brackets,
and inside here goes the condition.
So we said that we want the DataFrame
where the rating is greater than 4.
Therefore, we say column Rating is
greater, let's leave some space greater
than 4. If we execute that, we get a
DataFrame, and all these rows have a
rating of 4.5 or more. So not 4,
because it's greater, it's not greater
or equal. If it was greater or equal,
we'll also get the 4 ratings 4.0. But
this is not the case here. So this is
what we get. You see, for example, that
rows with index 0, 1, 2, were not included
in the DataFrame in the output DataFrame.
And you can see the length of
this using a len function at Python,
len function. So we get 29,758
rows that have a rating of greater than
4. Another way, if you don't want to
use a native Python function, another
way is to use count. So that is the DataFrame
object, and the DataFrame objects
have a count method. And you get this
output, which basically says that we
have this amount of values, in Course
Name this amount of values in Timestamp,
these amounts in Rating and,
and Comments. Because comments have this NaN
values, that is basically a non existent
value. It doesn't exist. Therefore, we
have 4927 ratings with comments where
people actually wrote something. So the
rest of this minus that gives us the
number of NaN value comments. Of
course, you can apply different types of
statistics to this now. For example,
this is a DataFrame, right. How do we
extract only the column? How do we select only
the column Rating? Well, you could
take a look at here, selecting a column.
So this is how you select a column data
Rating. Now, this is the DataFrame,
therefore you apply Rating, and you
get the ratings with a value of 4.5 or more.
So to understand this, what we do is, let's say d2
[No audio]
is the DataFrame that we get to based on that
condition, right. So it's a DataFrame,
that DataFrame has higher rating column. So that's
what we get. That's the same thing that we did
in here, data Rating, d2 Rating. Okay,
I hope that's clear. That also allows you to
apply functions such as mean. So you get
the mean of the ratings greater than
4, right. I'm going to leave it as it was.
And then next is
[No audio]
Multiple conditions.
[No audio]
Let's say we want to filter
the DataFrame where the rating is
greater than 4, and the Course Name
is equal to the complete Python Course,
Built 10 Professional OOP Apps.
Alright, so this course here, let me
copy the string, so I don't make any
typos. This is how you do that
filtering, double filtering, you start
with that, and then instead of putting
one condition as we did in here, we
would put two conditions which would be
separated inside parentheses. So the
first condition there, and the second
condition in there. So as the first
condition we put the same thing, so that's data
[No audio]
Rating is greater than 4. So
you see this, the condition is inside
the parenthesis. That's the first
condition. The second condition is where
data we set Course Name is equal to
the string, The Complete Python Course:
Build 10 Professional OOP Apps, right.
Execute, and we get a DataFrame based
on that filtering. So, since this is a
DataFrame.DataFrame, we can
extract rating out of that and then get
the mean of the rating column. And that
will give us this number. So the average
rating of the ratings above 4, '4.' course.
[No audio]
And this is the filter DataFrame again.
[No audio]
And that is above this lecture in the
next lecture, we are going to look at
filtering data based on times. So how
you can extract portions of your
DataFrame for a particular period. So you
see we have this Timestamp column here,
and that ranges from 2018 to 2021.
Let's see how you do that in the next video.