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.- reset_indexbool, optional (default:
True) If
True, the index is reset to a sequential range index. IfFalse, the original index is maintained.- kwargs
dict See the documentation for
pandas.eval()for complete details on the keyword arguments accepted bypandas.DataFrame.query().
- data
Notes
queryis 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], ... 'z': list('aabbcd')}) >>> df >> query('x % 2 == 0') x y z 0 0 0 a 1 2 1 b 2 4 2 c
>>> df >> query('x % 2 == 0 & y > 0') x y z 0 2 1 b 1 4 2 c
By default, Bitwise operators
&and|have the same precedence as the booleansandandor.>>> df >> query('x % 2 == 0 and y > 0') x y z 0 2 1 b 1 4 2 c
queryworks within groups>>> df >> query('x == x.min()') x y z 0 0 0 a
>>> df >> group_by('y') >> query('x == x.min()') groups: ['y'] x y z 0 0 0 a 1 2 1 b 2 4 2 c 3 5 3 d
When working with strings, the values should be quoted.
>>> df >> query('z == "a"') x y z 0 0 0 a 1 1 0 a
You can refer to variables in the environment by prefixing them with an @ character.
>>> w = list('rrbbst') >>> df >> query('z == @w') x y z 0 2 1 b 1 3 1 b
For more information see
pandas.DataFrame.query(). To query rows and columns withNaNvalues, usedropna