Skip to content

Instantly share code, notes, and snippets.

@franc3000
Created November 10, 2017 18:36
Show Gist options
  • Save franc3000/c9e7253aa1d3b213fbe12036ef75b9f2 to your computer and use it in GitHub Desktop.
Save franc3000/c9e7253aa1d3b213fbe12036ef75b9f2 to your computer and use it in GitHub Desktop.
pandas subsets
# To select rows whose column value equals a scalar, some_value, use ==:
df.loc[df['column_name'] == some_value]
# To select rows whose column value is in an iterable, some_values, use isin:
df.loc[df['column_name'].isin(some_values)]
# Combine multiple conditions with &:
df.loc[(df['column_name'] == some_value) & df['other_column'].isin(some_values)]
# To select rows whose column value does not equal some_value, use !=:
df.loc[df['column_name'] != some_value]
# isin returns a boolean Series, so to select rows whose value is not in some_values, negate the boolean Series using ~:
df.loc[~df['column_name'].isin(some_values)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment