# Select Rows From A DataFrame

There are numerous ways to select rows from a DataFrame. One method is to select rows based on the content of its columns. To do this, we can use conditions.

For our example, let's explore a DataFrame of different pets:

## Condition Operators

When using conditions, there are six primary comparison operators:

• < (strictly less than)
• > (strictly greater than)
• <= (less than or equal to)
• >= (greater than or equal to)
• == (exactly equal to)
• != (doesn't equal)

When you use a conditional by itself, a Series of True or False values based on the truth of the conditional is given:

## Row Selection With a Single Condition

To select only rows that match one specific criteria, we can use a single condition.

For example, say were only interested in looking at amphibian pets:

Now, say we are only interested in smaller pets that weighed less than a pound:

Additional explanations, videos, and example problems covering conditionals is part of the DISCOVERY course content found here:

## Row Selection with Multiple Conditions

It is possible to select rows that meet different criteria using multiple conditions by joining conditionals together with & (AND) or | (OR) logical operators. (Note: Python requires the use of parentheses around the conditionals when using multiple conditionals!)

For example, say we want a pet that lives longer than 10 years but less than 15 years.

### Row Section with Mixed Logical Operators

Now, say we wanted to look at pets that is either a mammal or an amphibian, and lives more than 12 years.

We have 3 conditions:

• df['group'] == 'amphibian'
• df['group'] == 'mammal'
• df['lifespan(yr.)'] > 12

But, notice the difference in output when these conditions are arranged differently:

This code first checks if the row's group column contains the word 'amphibian'.
If not, then it checks if the row contains the word 'mammal' in the group column and contains a value greater than 12 in the lifespan(yr.) column.
# putting the lifespan condition last
df[(df['group'] == 'amphibian') | (df['group'] == 'mammal') & (df['lifespan(yr.)'] > 12)]
nameweight(lb.)lifespan(yr.)group
2axolotl0.6312amphibian
4frog0.8011amphibian
6salamander0.4416amphibian
7chinchilla1.8018mammal

This code first checks if the row's lifespan(yr.) column contains a value greater than 12 and its group column contains the word 'amphibian'. If not, then it checks if the row's group column contains the word 'mammal'.
# putting the lifespan condition first
df[(df['lifespan(yr.)'] > 12) & (df['group'] == 'amphibian') | (df['group'] == 'mammal')]
nameweight(lb.)lifespan(yr.)group
0golden retriever70.0011mammal
1ferret4.407mammal
6salamander0.4416amphibian
7chinchilla1.8018mammal

This line of code checks each row for a value greater than 12 in the lifespan(yr.) column and that its group column contains either amphibian or mammal.
# The same as the code above, except there are parenthesis surrounding the 2nd and 3rd condition
df[(df['lifespan(yr.)'] > 12) & ((df['group'] == 'amphibian') | (df['group'] == 'mammal'))]
nameweight(lb.)lifespan(yr.)group
6salamander0.4416amphibian
7chinchilla1.8018mammal

Notice that the order of the conditions, the placement of parenthesis, and the use of the logical operators change the output.

### Row Selection with Five Conditionals

Finally, when selecting rows from a DataFrame, we can add as many conditions as we want:

An explanation of how AND and OR operators work, including videos, example problems, and more details is part of the DISCOVERY course content: