Created
November 10, 2017 18:36
-
-
Save franc3000/c9e7253aa1d3b213fbe12036ef75b9f2 to your computer and use it in GitHub Desktop.
pandas subsets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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