Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataFrame: add column with the size of a group

I have the following dataframe:

    fsq digits  digits_type
0    1   1       odd
1    2   1       odd
2    3   1       odd
3    11  2       even
4    22  2       even
5    101 3       odd
6    111 3       odd

and I want to add a last column, count, containing the number of fsq belonging to the digits group, i.e:

    fsq digits  digits_type   count
0    1   1       odd          3
1    2   1       odd          3
2    3   1       odd          3
3    11  2       even         2
4    22  2       even         2
5    101 3       odd          2
6    111 3       odd          2

Since there are 3 fsq rows that has digits equal to 1, 2 fsq rows that has digits equal to 2, etc.

like image 867
luffe Avatar asked Apr 11 '14 16:04

luffe


2 Answers

In [395]: df['count'] = df.groupby('digits')['fsq'].transform(len)

In [396]: df
Out[396]: 
   fsq  digits digits_type  count
0    1       1         odd      3
1    2       1         odd      3
2    3       1         odd      3
3   11       2        even      2
4   22       2        even      2
5  101       3         odd      2
6  111       3         odd      2

[7 rows x 4 columns]
like image 106
TomAugspurger Avatar answered Oct 21 '22 18:10

TomAugspurger


In general, you should use Pandas-defined methods, where possible. This will often be more efficient.

In this case you can use 'size', in the same vein as df.groupby('digits')['fsq'].size():

df = pd.concat([df]*10000)

%timeit df.groupby('digits')['fsq'].transform('size')  # 3.44 ms per loop
%timeit df.groupby('digits')['fsq'].transform(len)     # 11.6 ms per loop
like image 7
jpp Avatar answered Oct 21 '22 20:10

jpp