Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 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

>Solution :

Try this:

df['Date'] = df['Date'].astype(str).apply(lambda x: str(x[0:3])+str(x[-2:]))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading