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

How to modify date value to only keep month value

I have a df, you can have it by running:

import pandas as pd
from io import StringIO
    
df = """  
           case_id    scheduled_date        code
           1213       2021-08-17            1
           3444       2021-06-24            3
           4566       2021-07-20            5
          
    """
df= pd.read_csv(StringIO(df.strip()), sep='\s\s+', engine='python')

How can I change scheduled_date to only keep year and month? The output should be:

  case_id   scheduled_date  code
0   1213    2021-08         1
1   3444    2021-06         3
2   4566    2021-07         5

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

>Solution :

You can also try this:

df['scheduled_date'] = pd.to_datetime(df.scheduled_date, format='%Y-%m-%d').dt.strftime('%Y-%m')


   case_id scheduled_date  code
0     1213        2021-08     1
1     3444        2021-06     3
2     4566        2021-07     5
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