简体   繁体   中英

change month-year format to year-month-date format in python

Am having a CSV file as input, In which one of the column has date format as Year-Month. I need to change the column format as Year-Month-Date Format, which implies month end date. Am using Python 3. Further, Also am using the modified column in aggregation function and group by function.

Eg: 2020-01

2020-02

2020-03

2020-04

Expected Result:

2020-01-31

2020-02-29 ( Considering leap Year )

2020-03-31

2020-04-30

so on...

from calendar import monthrange
from datetime import date
def month_end(year, month):
    return date(year=year, month=month, day=monthrange(year, month)[1]).isoformat()
>>> month_end(2020, 2)
'2020-02-29'
import pandas as pd
import datetime as dt
import calendar as cal

# making data frame from csv file 
df = pd.read_csv("Table.csv")

# (month_year)->Column Name
df['month_last_dates'] = [datetime.datetime(date.year, date.month,
      calendar.monthrange(date.year, date.month)[1]) for date in month_year]

# defining aggregation process for each column
aggregations={ 'Metric':sum }

# Group by and aggregate                              
print( df.groupby(['col1', 'month_last_dates','col2']).agg(aggregations) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM