I have a pandas dataframe:
df
0 PL
1 PL
2 PL
3 IT
4 IT
..
4670 DE
4671 NO
4672 MT
4673 FI
4674 XX
Name: country_code, Length: 4675, dtype: object
I am filtering this by germany country tag 'DE' via:
df = df[df.apply(lambda x: 'DE' in x)]
If I would like to filter with more countries than I have to add them manually via: .apply(lambda x: 'DE' in x or 'GB' in x). However I would like to create a countries list and generate this statement automaticly.
Something like this:
countries = ['DE', 'GB', 'IT']
df = df[df.apply(lambda x: any_item_in_countries_list in x)]
I think I can filter df 3 times and then merge these pieces back via concat(), however is there a more generic function to achieve this?