英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论