in this video we're going to learn how
to select
columns in a pandas data frame based on
a logical condition
across the values of the columns so for
instance what if we were only interested
in looking at
columns of this data set where the
average value of the column
was greater than a certain number for
maybe we're just not interested
in columns that tend to have low values
well we could do that using a logical
indexing operation over the columns so
in that
case let's say we wanted to get only
columns where the average of that column
was greater than 10.
we could do that with a logical indexing
operation
what we want to do is start by finding
the
column means so we can do that by
passing in data frame dot mean and we're
going to say
we want the columns where the mean of
that column is greater than 10
and we'll just store this as logical
index and now we can see that we have
a logical index here where every column
has a true false value assigned to it
based on whether this was true or not
now we can use this logical index
to go back into our data frame and only
keep the columns where this was true
so we'll show how to do that so to do
that we can say
empty cars.columns so that's getting us
the
list of columns and then we're going to
use the logical index to index into this
so after we do that we'll store that as
calls
the remaining columns here will be the
ones where our logical check above
was true now once we have these columns
in our hand the ones that we want to
subset
we can just use that pass it back into
the indexer for the data frame
then we'll be left with a data frame
subset that is only those columns where
that was true
so we will run this and just check the
first few rows to
see that we did cut down the number of
columns now we are only left with
columns where the average value was
greater than 10.
now all the code used in this video is
available in the description below
if you found that video useful you can
drop a like or hit subscribe
and thanks for watching