Filter dataframe with complex expression Blocked version of pd.DataFrame.query Parameters ---------- expr: str The query string to evaluate. You can refer to column names that are not valid Python variable names by surrounding the
(self, expr, **kwargs)
| 3607 | ) |
| 3608 | |
| 3609 | def query(self, expr, **kwargs): |
| 3610 | """Filter dataframe with complex expression |
| 3611 | |
| 3612 | Blocked version of pd.DataFrame.query |
| 3613 | |
| 3614 | Parameters |
| 3615 | ---------- |
| 3616 | expr: str |
| 3617 | The query string to evaluate. |
| 3618 | You can refer to column names that are not valid Python variable names |
| 3619 | by surrounding them in backticks. |
| 3620 | Dask does not fully support referring to variables using the '@' character, |
| 3621 | use f-strings or the ``local_dict`` keyword argument instead. |
| 3622 | |
| 3623 | See also |
| 3624 | -------- |
| 3625 | pandas.DataFrame.query |
| 3626 | pandas.eval |
| 3627 | |
| 3628 | Examples |
| 3629 | -------- |
| 3630 | >>> import pandas as pd |
| 3631 | >>> import dask.dataframe as dd |
| 3632 | >>> df = pd.DataFrame({'x': [1, 2, 1, 2], |
| 3633 | ... 'y': [1, 2, 3, 4], |
| 3634 | ... 'z z': [4, 3, 2, 1]}) |
| 3635 | >>> ddf = dd.from_pandas(df, npartitions=2) |
| 3636 | |
| 3637 | Refer to column names directly: |
| 3638 | |
| 3639 | >>> ddf.query('y > x').compute() |
| 3640 | x y z z |
| 3641 | 2 1 3 2 |
| 3642 | 3 2 4 1 |
| 3643 | |
| 3644 | Refer to column name using backticks: |
| 3645 | |
| 3646 | >>> ddf.query('`z z` > x').compute() |
| 3647 | x y z z |
| 3648 | 0 1 1 4 |
| 3649 | 1 2 2 3 |
| 3650 | 2 1 3 2 |
| 3651 | |
| 3652 | Refer to variable name using f-strings: |
| 3653 | |
| 3654 | >>> value = 1 |
| 3655 | >>> ddf.query(f'x == {value}').compute() |
| 3656 | x y z z |
| 3657 | 0 1 1 4 |
| 3658 | 2 1 3 2 |
| 3659 | |
| 3660 | Refer to variable name using ``local_dict``: |
| 3661 | |
| 3662 | >>> ddf.query('x == @value', local_dict={"value": value}).compute() |
| 3663 | x y z z |
| 3664 | 0 1 1 4 |
| 3665 | 2 1 3 2 |
| 3666 | """ |