0

I have the following type of input data say df1 :

idtag alpha type
1      abc   1a
1      avg   1a
1      rgw   2a
1      rrw   2a
2      rgw   2a
2      abc   1a
3      abc   1a
3      rqw   2a

The requirement is to retrieve the "type" and count of each "type" value for every "idtag" value into a new df2 like shown below:

idtag  1a  2a 
1      2    2
2      1    1
3      1    1

This is what I used for groupby:

df1.groupby(['idtag','type']).count()

But, I am having trouble in shaping the results into producing df2. Appreciate the help.

Thank you.

savi
  • 323
  • 1
  • 11
  • and for you it is the question number 9 of the duplicate link :) – Ben.T Jul 14 '20 at 13:05
  • Thank Ben. So, I can delete this post ? – savi Jul 14 '20 at 13:17
  • Not especially, duplicates question can redirect other users to the original post with the link at the top of the question, if someone do a research with the same key word than in your title, it will help them to be redirected :) – Ben.T Jul 14 '20 at 13:30

1 Answers1

0
df.pivot_table(index='idtag',columns='type', values='alpha', aggfunc='count')
Chris
  • 15,819
  • 3
  • 24
  • 37
  • Thank you, this worked. But, I have been told that its a duplicate question. But, thank you for your help. – savi Jul 14 '20 at 13:17