40 Selecting Rows and Columns in Pandas
To allow all the exercises in this section to work, please run this code cell first!
This will import pandas and load the dataframe we’ll be working with.
40.1 Indexing
In Pandas, we can select certain rows and / or columns of data in our DataFrame - this is known as Indexing. Let’s look at some examples using our DataFrame.
40.1.1 Retrieve a given column
When we retrieve a given column, note the index is always returned too:
40.1.2 Retrieve multiple columns
When we retrieve a given column, note the index is always returned too:
40.1.3 Retrieve a given row (record) (by providing the unique index value)
40.1.4 Retrieve multiple given rows
40.1.5 Retrieve multiple given rows and only specified columns
40.2 Conditional Indexing
We can also select rows in a DataFrame that meet certain criteria by using conditional logic.
40.2.1 Retrieve all records meeting a single criteria
Here, we’ll retrieve all records where the county is Cornwall
The following - not using .loc - will also work.
How does this work? Let’s first look at what happens when we just run the part inside the square brackets.
We just get a list of ‘true’ and ‘false’ because it’s looking at the ‘county’ value for every row and returning True
if it matches “Cornwall”, and False
otherwise.
This can be called a mask
.
When we apply this mask
to our dataframe, it returns only the rows where the value in the mask was True.
40.2.2 More conditional indexing
This time, instead of looking for exact matches, we’ll look for something that exceeds a numeric value.
40.2.3 Multiple Conditions (and)
Let’s now retrieve all records for patients in Cornwall aged 60 and over.
You must use ampersand (&) not the word and here
We could also write this all on one line.
Note that you will need brackets around each separate condition when doing this.
40.2.4 Multiple Conditions (or)
Let’s retrieve all records where either the patient lives in Cornwall or they’re aged 60 or over.
You must use pipe (|) not the word or here
We could also write this all on one line.