*32*

You can use the following basic syntax to select rows in a pandas DataFrame based on values in a boolean series:

#define boolean series bools = pd.Series([True, False, True, True, False, False, False, True]) #select rows in DataFrame based on values in boolean series df[bools.values]

This allows you to select each of the rows in the pandas DataFrame where the corresponding value in the boolean series is **True**.

The following example shows how to use this syntax in practice.

**Example: Select Rows from Pandas DataFrame Using Boolean Series**

Suppose we have the following pandas DataFrame that contains information about various basketball players:

import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'], 'points': [18, 22, 19, 14, 14, 11, 20, 28], 'assists': [5, 7, 7, 9, 12, 9, 9, 4], 'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]}) #view DataFrame print(df) team points assists rebounds 0 A 18 5 11 1 B 22 7 8 2 C 19 7 10 3 D 14 9 6 4 E 14 12 6 5 F 11 9 5 6 G 20 9 9 7 H 28 4 12

We can use the following syntax to select all rows in the DataFrame where the corresponding value in a boolean series is **True**:

#define boolean series bools = pd.Series([True, False, True, True, False, False, False, True]) #select rows in DataFrame based on values in boolean series df[bools.values] team points assists rebounds 0 A 18 5 11 2 C 19 7 10 3 D 14 9 6 7 H 28 4 12

Notice that the only rows returned are the ones where the corresponding value in the boolean series is **True**.

Also note that you can use the following syntax to only select the rows in the “points” column of the DataFrame where the corresponding value in the boolean series is **True**.

#define boolean series bools = pd.Series([True, False, True, True, False, False, False, True]) #select rows in points column based on values in boolean series df['points'][bools.values] 0 18 2 19 3 14 7 28 Name: points, dtype: int64

Notice that the only rows returned from the “points” column are the ones where the corresponding value in the boolean series is **True**.

**Additional Resources**

The following tutorials explain how to perform other common tasks in pandas:

How to Filter Rows Based on String Length in Pandas

How to Select Rows without NaN Values in Pandas

How to Select Rows Based on Column Values in Pandas