我在DF中有一个日期列,类似于"FY19 Jun">。如何将数据格式转换为 2019-01-01
IIUC,使用:
df = pd.DataFrame({'date': ['FY19 Jun']})
df['converted'] = pd.to_datetime(df['date'], format='FY%y %b')
输出:
date converted
0 FY19 Jun 2019-06-01
如果你想要年初,忽略月份:
df['converted'] = pd.to_datetime(df['date'].str.extract(r'FY(d+)', expand=False), format='%y')
输出:
date converted
0 FY19 Jun 2019-01-01