plydata.one_table_verbs.query¶
-
class
plydata.one_table_verbs.
query
(*args, **kwargs)[source]¶ Return rows with matching conditions
- Parameters
- data
dataframe
, optional Useful when not using the
>>
operator.- expr
str
The query string to evaluate. You can refer to variables in the environment by prefixing them with an '@' character like
@a + b
. Allowed functions are sin, cos, exp, log, expm1, log1p, sqrt, sinh, cosh, tanh, arcsin, arccos, arctan, arccosh, arcsinh, arctanh, abs and arctan2.- kwargs
dict
See the documentation for
pandas.eval()
for complete details on the keyword arguments accepted bypandas.DataFrame.query()
.
- data
Notes
query
is the equivalent of dplyr's filter verb but with slightly different python syntax the expressions.Examples
>>> import pandas as pd >>> df = pd.DataFrame({'x': [0, 1, 2, 3, 4, 5], ... 'y': [0, 0, 1, 1, 2, 3]}) >>> df >> query('x % 2 == 0') x y 0 0 0 2 2 1 4 4 2
>>> df >> query('x % 2 == 0 & y > 0') x y 2 2 1 4 4 2
By default, Bitwise operators
&
and|
have the same precedence as the booleansand
andor
.>>> df >> query('x % 2 == 0 and y > 0') x y 2 2 1 4 4 2
query
works within groups>>> df >> query('x == x.min()') x y 0 0 0
>>> df >> group_by('y') >> query('x == x.min()') groups: ['y'] x y 0 0 0 2 2 1 4 4 2 5 5 3
For more information see
pandas.DataFrame.query()
. To query rows and columns withNaN
values, usedropna