How can I choose several values at once in data frame?

Viewed 31
df[(df["Variable"]=="% Recycling") & (df["Country"]== ["United Kingdom", "Türkiye", "OECD - Total"])]

I could not understand why would this code give errors.

1 Answers

Try isin; you also need .loc:

df.loc[df["Variable"].eq("% Recycling") 
   & df["Country"].isin(["United Kingdom", "Türkiye", "OECD - Total"])]

or try with query:

df.query("Variable == '% Recycling' and Country in ['United Kingdom', 'Türkiye', 'OECD - Total']")
Related