Skip to content

Instantly share code, notes, and snippets.

@SolClover
Created August 8, 2020 02:57
Show Gist options
  • Save SolClover/765271ce0f21a4353997bc3714b7295d to your computer and use it in GitHub Desktop.
Save SolClover/765271ce0f21a4353997bc3714b7295d to your computer and use it in GitHub Desktop.
Filter Pandas DataFrame
# Filter on one condition
new_df=df[df['Col_A']>=6]
# Filter on multiple conditions using AND
new_df=df[(df['Col_A']>=6) & (df['Col_B']==5)]
# Filter on multiple conditions using OR
new_df=df[(df['Col_A']>=6) | (df['Col_B']==1)]
# Create a list and use isin to filter
filter_list=['1.apple', '3.pear']
new_df=df[df['fruit'].isin(filter_list)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment