Filter a Pandas DataFrame Using a List

Python
Data Preprocessing

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']
 2|  
 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)]
1 Upvote
Tags: Filter | Isin
Did you find this snippet useful?

Sign up to bookmark this in your snippet library

Normalize Windowed Time Series
Python
Data Preprocessing

Scaler | Normalize | Scale | Min-max

4
Pivoting Pandas Dataframes
Python
Data Preprocessing

Pandas

3