1

I am an R user and learning python pandas. I am wondering how to covert to a data frame of count number of products that per agent has in Python Pandas.

Data:
    EVT_ID  CUST_KEY    AGT_KEY  product_group
0   1-2U    4624429     6088722  Other_product
1   1-2B    4588790     5443951  Cold
2   1-2T    4368207     6343767  Hot
3   1-2G    4615587     6294539  Warm

Convert :

AGT_KEY    other_product   Cold   Hot  warm
1-2U            1           0      0     0
1-2B            0           1      0     0  
1-2T            0           0      1     0
1-2G            0           0      0     1

R code:

df<-as.data.frame(mydata%>%group_by(AGT_KEY)%>%count(product_group)%>%
spread(product_group,n))

Python i wrote:

num_offers_agent_level=agents[['ADE_AGT_CO_REP_KEY','Offer_group3']].pivot_table(index=['ADE_AGT_CO_REP_KEY'],columns='Offer_group3',aggfunc='count')
rafaelc
  • 57,686
  • 15
  • 58
  • 82
Jimmy
  • 11
  • 2

0 Answers0