利用pandas计算一月至今的所有月份(欢迎评论区留下其他方法)

利用pandas计算一月至今的所有月份(欢迎评论区留下其他方法),第1张

利用pandas计算一月至今的所有月份(欢迎评论区留下其他方法)
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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5710558.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-17
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存