2017-02-05 31 views
0

我有一個數據集象下面這樣:確切行的頻率計數

Col1. Col2. Col3.  Col4 
A  123  D   J 
B  234  E   M 
A  234  D   J 

我需要補充的是,對剛剛列COL1 COL3和COL4正好精確匹配的頻率計數第5列。

預期輸出:

Col1. Col2. Col3.  Col4. Col5 
A  123  D   J.  2 
B  234  E   M.  1 
A  234  D   J.  2 

回答

1

可以使用groupby + transform('count')

In [70]: df['Col5'] = df.groupby(['Col1','Col3'])['Col2'].transform('count') 

In [71]: df 
Out[71]: 
    Col1 Col2 Col3 Col4 Col5 
0 A 123 D J  2 
1 B 234 E M  1 
2 A 234 D J  2