plydata.one_table_verbs.query

class plydata.one_table_verbs.query(*args, **kwargs)[source]

Return rows with matching conditions

Parameters
datadataframe, optional

Useful when not using the >> operator.

exprstr

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.

kwargsdict

See the documentation for pandas.eval() for complete details on the keyword arguments accepted by pandas.DataFrame.query().

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 booleans and and or.

>>> 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 with NaN values, use dropna