Filter a Pandas DataFrame Using a List


To use a list to filter a Pandas DataFrame we can make use of the isin function and pass a list of values to keep.

In the example below we pass a list called "values" containing the two values from the Embarked column in our DataFrame that we want to filter for.

 1|  values = ['Q', 'C']
 3|  # df will contain only rows that have either a 'Q' or 'C' in
 4|  # the Embarked column
 5|  df = df[df['Embarked'].isin(values)]
Did you find this snippet useful?

Sign up for free to to add this to your code library