0

I am doing Cohort analysis for my company which consists of 2.5 year data. When constructing Pivot tables for cohort table visualizations, my order of columns get shifted (look at img).Cohort data

I guess problem is in proper naming (I should somehow add another 0 before single digit values).

users['monthnum'] = users.OrderDateTime.apply(lambda x: x.month + 12*(x.year-2016))
users['running_month'] = users.monthnum - users.cohort_monthnum
users.running_month = users.running_month.astype(str) + '-month'

I am a newbie to data analysis in python so I am not sure how to fix this. I know if I just leave last row of code left out (so I am not making it a string #purpose is for naming for employer) cohorts work just fine (as integers are sorted correctly).

pirho
  • 11,565
  • 12
  • 43
  • 70
  • 1
    Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Feb 23 '18 at 07:52

1 Answers1

0

Use str.zfill:

users.running_month = users.running_month.astype(str).str.zfill(2) + '-month'
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252