0

I have a df with information about sales per month-year like this:

sales     month-year
2         2017-01
5         2017-01
4         2017-02
3         2017-03
3         2017-03

I want to add a consecutive month number like this:

 sales     month-year   month-number
    2         2017-01   1
    5         2017-01   1
    4         2017-02   2
    3         2017-03   3
    3         2017-03   3

The original df contains data for multiple years, so 2018-01 should be 13 etc..

What is the best way to do this?

Thanks in advance

ursteiner
  • 149
  • 6

1 Answers1

1

Try this! Pandas - Number of Months Between Two Dates

df['month-number'] = ((df["month-year"] - df["month-year"][0])/np.timedelta64(1, 'M')).astype(int)
Dustin
  • 483
  • 3
  • 13