import pandas as pd df = pd.DataFrame({'country': ['Italy', 'Greece', 'Japan', 'Germany']}) countries_to_keep = ['Japan', 'Italy'] df
country
0 Italy
1 Greece
2 Japan
3 Germany
The usual way to filter the Dataframe is by using the ‘isin’ function:
df[df.country.isin(selected_countries)]
country
0 Italy
2 Japan
Now, If we want to keep the order of the list we can do the following:
ind=[df.index[df['country']==i].tolist() for i in selected_countries] flat_ind=[item for sublist in ind for item in sublist] df.reindex(flat_ind)
country
2 Japan
0 Italy