plydata.helper_verbs.tally¶
-
class
plydata.helper_verbs.
tally
(*args, **kwargs)[source]¶ Tally observations by group
tally
is a convenient wrapper for summarise that will either calln
orsum(n)
depending on whether you're tallying for the first time, or re-tallying.- Parameters
- data
dataframe
, optional Useful when not using the
>>
operator.- weights
str
or array-like, optional Weight of each row in the group.
- sortbool, optional
If
True
, sort the resulting data in descending order.
- data
Examples
>>> import pandas as pd >>> from plydata import tally, group_by, summarize >>> df = pd.DataFrame({ ... 'x': [1, 2, 3, 4, 5, 6], ... 'y': ['a', 'b', 'a', 'b', 'a', 'b'], ... 'w': [1, 2, 1, 2, 1, 2]})
Without groups it is one large group
>>> df >> tally() n 0 6
Sum of the weights
>>> df >> tally('w') n 0 9
With groups
>>> df >> group_by('y') >> tally() y n 0 a 3 1 b 3
With groups and weights
>>> df >> group_by('y') >> tally('w') y n 0 a 3 1 b 6
Applying the weights to a column
>>> df >> group_by('y') >> tally('x*w') y n 0 a 9 1 b 24
You can do that with
summarize
>>> df >> group_by('y') >> summarize(n='sum(x*w)') y n 0 a 9 1 b 24