提取月份和实际年份从月份期间格式。

huangapple go评论60阅读模式
英文:

Extract Month and actual year from a Month Period Format

问题

从包含格式为“June 2021-22”或“January 2021-22”的列的一个数据框中考虑,我如何解析出月份和实际年份?

这个问题与以往不同,因为这里的值中年份以期间格式(“2022-23”)给出,而不是一个直接的年份。因此,实际值将取决于考虑的月份,基于财政年度。

例如,在这种情况下,值应该是2021年6月和2022年1月。感谢帮助!

英文:

Considering one dataframe with column which has values of the format " June 2021-22" or "January 2021-22", how do I parse Month and actual year from it?

This question is different because here we have values where year is i a period format ( "2022-23") and not a straightforward year. Thus the actual value will depend on the month considered based on the financial year.

Eg, in this case value should be June 2021 and January 2022. Appreciate the help !

答案1

得分: 1

df['date1'] = pd.to_datetime(df['date']).dt.strftime('%B %Y')
print (df)
              date         date1
0     June 2021-22     June 2021
1  January 2021-22  January 2021
英文:

Use:

df['date1'] = pd.to_datetime(df['date']).dt.strftime('%B %Y')
print (df)
              date         date1
0     June 2021-22     June 2021
1  January 2021-22  January 2021

huangapple
  • 本文由 发表于 2023年6月26日 16:18:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76554801.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定