plydata.helper_verbs.add_count

class plydata.helper_verbs.add_count(*args, **kwargs)[source]

Add column with number of items in each group

Similar to count, but it adds a column and does not collapse the groups. It is also a shortcut of add_tally that does the grouping.

Parameters
datadataframe, optional

Useful when not using the >> operator.

*argsstr, list

Columns to group by.

weightsstr or array-like, optional

Weight of each row in the group.

sortbool, optional

If True, sort the resulting data in descending order.

See also

add_tally

Examples

>>> import pandas as pd
>>> from plydata import *
>>> 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 >> add_count()
   x  y  w  n
0  1  a  1  6
1  2  b  2  6
2  3  a  1  6
3  4  b  2  6
4  5  a  1  6
5  6  b  2  6

Sum of the weights

>>> df >> add_count(weights='w')
   x  y  w  n
0  1  a  1  9
1  2  b  2  9
2  3  a  1  9
3  4  b  2  9
4  5  a  1  9
5  6  b  2  9

With groups

>>> df >> add_count('y')
   x  y  w  n
0  1  a  1  3
1  2  b  2  3
2  3  a  1  3
3  4  b  2  3
4  5  a  1  3
5  6  b  2  3
>>> df >> group_by('y') >> add_count()
groups: ['y']
   x  y  w  n
0  1  a  1  3
1  2  b  2  3
2  3  a  1  3
3  4  b  2  3
4  5  a  1  3
5  6  b  2  3

With groups and weights

>>> df >> add_count('y', weights='w')
   x  y  w  n
0  1  a  1  3
1  2  b  2  6
2  3  a  1  3
3  4  b  2  6
4  5  a  1  3
5  6  b  2  6

Applying the weights to a column

>>> df >> add_count('y', weights='x*w')
   x  y  w   n
0  1  a  1   9
1  2  b  2  24
2  3  a  1   9
3  4  b  2  24
4  5  a  1   9
5  6  b  2  24

You can do that with add_tally

>>> df >> group_by('y') >> add_tally('x*w') >> ungroup()
   x  y  w   n
0  1  a  1   9
1  2  b  2  24
2  3  a  1   9
3  4  b  2  24
4  5  a  1   9
5  6  b  2  24