Skip to content

Instantly share code, notes, and snippets.

@sizhky
Created April 20, 2023 05:17
Show Gist options
  • Save sizhky/6f18fc31b6d5a84eddc7d0b5d2386b70 to your computer and use it in GitHub Desktop.
Save sizhky/6f18fc31b6d5a84eddc7d0b5d2386b70 to your computer and use it in GitHub Desktop.
Sure, here is a summary of the 20 examples of using `pd.DataFrame.query()` to filter data:
1. Filter on exact string match: `df.query('name == "Alice"')`
2. Filter on multiple conditions: `df.query('age > 30 and city == "New York"')`
3. Filter on a list of values: `df.query('age in [25, 30, 35]')`
4. Filter on a range of values: `df.query('age.between(25, 35)')`
5. Filter on string contains: `df.query('name.str.contains("an")')`
6. Filter on string starts with: `df.query('name.str.startswith("A")')`
7. Filter on string ends with: `df.query('name.str.endswith("a")')`
8. Filter on regular expression: `df.query('name.str.match("^[Aa].*a$")')`
9. Filter on null values: `df.query('city.isna()')`
10. Filter on column names with spaces: `df.query('`column name` > 5')`
11. Filter on a calculated column: `df.query('salary * 1.1 > bonus')`
12. Filter on a column with spaces in the name: `df.query('`column name` == "value"')`
13. Filter on a datetime column: `df.query('date > "2022-01-01"')`
14. Filter on a timedelta column: `df.query('delta > "1 day"')`
15. Filter on a column with nested data: `df.query('col["key"] == "value"')`
16. Filter on a column that contains lists: `df.query('tags.apply(lambda x: "fruit" in x)')`
17. Filter on a column that contains dictionaries: `df.query('metadata.apply(lambda x: x.get("type", "") == "document")')`
18. Filter on a column with a window function: `df.query('rolling_mean > 30')`
19. Filter on a column with a custom function: `df.query('col.apply(lambda x: my_func(x) > 5)')`
20. Filter on a column with a SQL-like query: `df.query('SELECT * FROM df WHERE col1 = "value1" AND col2 > 10')`
These examples cover a wide range of scenarios and can be used as a starting point for customizing and building more complex queries to filter pandas dataframes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment