【问题标题】:Add the first day of each month to year month date in python在python中将每个月的第一天添加到年份月份日期
【发布时间】:2022-01-19 09:31:00
【问题描述】:

我有一个包含 3 列年月和 SPEI 值的数据框 我想添加包含每个月的第一天的日期列

【问题讨论】:

  • 你能从样本数据中添加预期的输出吗?

标签: python-3.x pandas python-datetime


【解决方案1】:

如果需要带有第一个 day 的新列,请使用:

df['day'] = 1

如果需要第一天的日期时间:

df['datetime'] = pd.to_datetime(df[['year','month']].assign(day=1))

【讨论】:

    猜你喜欢
    • 2020-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多