102 - Sorting and Subsetting - Python
102 - Sorting and Subsetting - Python
2. Sorting
The first thing you can do is change the order of the rows by sorting them so that the most
interesting data is at the top of the DataFrame. You can sort rows using the sort_values method,
passing in a column name that you want to sort by. For example, when we apply sort_values on
the weight_kg column of the dogs DataFrame, we get the lightest dog at the top, Stella the
Chihuahua, and the heaviest dog at the bottom, Bernie the Saint Bernard.
6. Subsetting columns
We may want to zoom in on just one column. We can do this using the name of the DataFrame,
followed by square brackets with a column name inside. Here, we can look at just the name
column.
8. Subsetting rows
There are lots of different ways to subset rows. The most common way to do this is by creating a
logical condition to filter against. For example, let's find all the dogs whose height is greater than 50
centimeters. Now we have a True or False value for every row.
9. Subsetting rows
We can use the logical condition inside of square brackets to subset the rows we're interested in to
get all of the dogs taller than 50 centimeters.
We can also subset based on dates. Here, we filter all the dogs born before 2015. Notice that the
dates are in quotes and are written as year then month, then day. This is the international standard
date format.
https://campus.datacamp.com/courses/data-manipulation-with-pandas/transforming-data?ex=4 2/2