Select rows by boolean expressions

You can make complex boolean (true/false) expressions to select or filter rows in your DataFrame. For example, if our DataFrame had a column age, we could select people who are 16 or older by doing df[df.age >= 16]. But suppose we wanted people who are 16 and older and live in New York. Then we could select them by doing df[(df.age >= 16) & (df.city == 'New York')]. The & in that expression means "and", | means "or" and ~ means "not". Unfortunately, you can't use the python operators and, or, and not with Pandas objects.

One gotcha about using & (and) and | (or) to make boolean expressions in Pandas is that you need to wrap each piece between a & or | in parenthesis. This is an unfortunate issue due to the way python evaluates operators. If you are interested, you can read more about it here. In either case, remember to wrap each piece in parenthesis!

Suppose you constructed a DataFrame by

import pandas as pd

df = pd.DataFrame({'name': ['Jeff', 'Esha', 'Jia'], 
                   'age': [30, 56, 8],
                   'city': ['New York', 'Atlanta', 'Shanghai']})

Giving you the DataFrame

name age city
0 Jeff 30 New York
1 Esha 56 Atlanta
2 Jia 8 Shanghai

Complete the function, select_target_audience(df), by having it return the rows with people who are between 21 and 40 years old (inclusive) and live in New York or Tokyo.

Example Input

Code to generate input

df = pd.DataFrame({'name': ['Jeff', 'Esha', 'Jia', 'Hatori', 'Ashley'], 
                   'age': [30, 56, 8, 38, 20],
                   'city': ['New York', 'Atlanta', 'Shanghai', 'Tokyo', 'New York']})


Table generated

name age city
0 Jeff 30 New York
1 Esha 56 Atlanta
2 Jia 8 Shanghai
3 Hatori 38 Tokyo
4 Ashley 20 New York

Example Output

name age city
0 Jeff 30 New York
3 Hatori 38 Tokyo