简体   繁体   English

在 python 中将每个月的第一天添加到年月日期

[英]Add the first day of each month to year month date in python

I have a dataframe that contains 3 columns year month and SPEI value I want to add day column that contains the first day of each month我有一个 dataframe 包含 3 列年月和 SPEI 值我想添加包含每个月的第一天的日列
这是一个例子

If need new column with first day use:如果需要第一day使用的新列:

df['day'] = 1

If need datetime with first day:如果需要第一天的日期时间:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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