Home > Mobile >  Reduce date values to reveal only 2 characters within the year (Pandas)
Reduce date values to reveal only 2 characters within the year (Pandas)

Time:02-21

I have a dataset where I would like to reduce a date column to reveal the YY vs YYYY

Data

ID      Date    Stat
AMS101  Q1.2022 y
BWI101  Q1.2022 y
BWI101  Q1.2022 n

Desired

 ID     Date    Stat
 AMS101 Q1.22   y
 BWI101 Q1.22   y
 BWI101 Q1.22   n

Doing

df['Date'] = df['Date'].astype(datetime).datetime[:-2]

Any suggestion is appreciated

CodePudding user response:

Try this:

df['Date'] = df['Date'].astype(str).apply(lambda x: str(x[0:3]) str(x[-2:]))
  • Related