user19562955
user19562955

Reputation: 293

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

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

I could not understand why would this code give errors.

Upvotes: 2

Views: 38

Answers (1)

mcsoini
mcsoini

Reputation: 6642

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']")

Upvotes: 2

Related Questions