-3

I have date column and I need to add month column in 'mmm-yy' Format in pandas

For eg.

Date Month 01/08/2022 Aug-22

  • Please dispaly usuable data also take a look at https://stackoverflow.com/questions/25146121/extracting-just-month-and-year-separately-from-pandas-datetime-column – INGl0R1AM0R1 Aug 02 '22 at 13:15
  • Almost the same was as [this answer](/a/39206377/15497888) except use lowercase "b" and lowercase "y" for mmm-yy format. (or with the dt accessor as suggested by [this comment](/posts/comments/107562834)) `df['mnth_yr'] = df['date_column'].dt.strftime('%b-%y')` – Henry Ecker Aug 02 '22 at 21:24

1 Answers1

0

Hello @vishal Valanju ,

please create a month wise dictionary like this,

dict = {'date': {
      0: '28-01-2022  5:25:00 PM',
      1: '27-02-2022  6:25:00 PM',
      2: '30-03-2022  7:25:00 PM',
      3: '29-04-2022  8:25:00 PM',
      4: '31-05-2022  9:25:00 PM'
    },
 'date_short': {0: 'Jan-2022', 1: 'Feb-2022', 2: 'Mar-2022', 3: 'Apr-2022', 4: 'May-2022'}}

df = pd.DataFrame(dict)

Happy Coding...!!

  • @vishal Valanju , if not clearly understand this code then you can also refer below link https://datascientyst.com/convert-string-to-datetime-pandas/ – Adwell Scott Aug 02 '22 at 13:25