-1

i'm wondering if there is a better way to slice a data frame with different criteria for the same variable.

actually i'm looking at this example: df_european_union = df[(df['Country'] == 'Austria')|(df['Country'] == 'Belgium')|(df['Country'] == 'Bulgaria')...

this works just fine but i don't think it's close to best practice.

already tried approaches like: df_european_union = df[(df['Country'] == ['Austria','Belgium', ... ]]

or eu = ['Austria','Belgium','Bulgaria',...] df_european_union = df[(df['Country'] == eu]

1 Answers1

1

There are many ways to do that. You can do that using query or isin pandas method like

df.loc[df.Contry.isin([liste_of_element]), :]

Or using query method like:

df.query("Country == 'name_of_country' | Country == 'another_name_of_country'")

I think it'll solve your problem. Thanks