python - Count the frequency of occurrence of a certain row -


suppose have data frame 3 columns as

     date        amount     type 0   20160101     50        apple  1   20160101     50        apple   2   20160101     50        banana  3   20160102     30        apple         4   20160102     50        apple 5   20160102     40        banana 6   20160102     40        banana  

what want count frequency of occurrence of row using columns, , result should like

date      amount      type      times 20160101   50         apple     2 20160101   50         banana    1 20160102   30         apple     1 20160102   50         apple     1 20160102   40         banana    2   

my code like

df out[23]:         date  amount    type 0  20160101      50   apple 1  20160101      50   apple 2  20160101      50  banana 3  20160102      30   apple 4  20160102      50   apple 5  20160102      40  banana 6  20160102      40  banana  p=df.pivot_table('amount','date','type')  p out[27]:  type      apple  banana date                    20160101     50      50 20160102     40      40 

here's hack answer. feel there needs more direct way though

df['times'] = 1 df.groupby(['date', 'amount', 'type'], as_index=false).sum() 

edit:

found second solution (you need rename column

df.groupby(['date','type']).amount.value_counts().reset_index()