
import pandas as pd
from datetime import datetime
# calculate the year and month from '2021-01-01' to now
df = pd.Dataframe(pd.date_range('2021-01-01', datetime.now(), freq='M'), columns=['begin_month'])
# offset the month by 1
df['end_month'] = df['begin_month'] + pd.DateOffset(months=1)
# revise the format of the month
df['begin_month'] = df['begin_month'].apply(lambda x: x.strftime('%Y-%m-01'))
df['end_month'] = df['end_month'].apply(lambda x: x.strftime('%Y-%m-01'))
print(df)
result
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)