isin, between and query
Three ways to say a condition that reads like the question.
isin for a set of values
Testing against several values with a chain of == and | works and reads badly. isin says it once.
import pandas as pd
df = pd.DataFrame({"subject": ["maths", "art", "computing"]})
print(df[df["subject"].isin(["maths", "computing"])].shape)
between for a range
between is inclusive at both ends by default, which is usually what a range in a sentence means.
import pandas as pd
df = pd.DataFrame({"score": [55, 70, 95]})
print(df[df["score"].between(60, 90)]["score"].tolist())
Exercise
Try It YourselfWrite in_subjects(df, subjects) returning the number of rows whose subject is one of subjects.
Press Run to see output