【发布时间】:2020-10-06 13:58:24
【问题描述】:
我有一个 pandas 数据框,其中一列(first_date)包含以下格式的日期:
2018-03-31
#I have created a monthly level data using the below code
dataset['first_date']=pd.to_datetime(dataset['first_date'])
dataset['first_date'].apply(lambda x: x.strftime('%Y-%m'))
**output: 2018-03**
我的问题: 使用 first_date 我想创建一个新列,以获取年份和季度。 输出类似于 2018-Q1 的内容,并且必须是字符串而不是 periods
【问题讨论】:
-
IIUC
df['first_date'].dt.to_period('Q') -
从你的代码我得到2018Q1,你能在年和Q之间加“-”