marcevgen Asked:2020-11-18 21:28:05 +0800 CST2020-11-18 21:28:05 +0800 CST 2020-11-18 21:28:05 +0800 CST 获取熊猫周数 772 数据框中有一列带有日期 date: | __________| 2019-10-12| __________| 2019-10-20| __________| 如何获取带有年、月和周的列? pandas 1 个回答 Voted Best Answer splash58 2020-11-18T22:03:18+08:002020-11-18T22:03:18+08:00 如果列不是日期时间类型,则为第一行 df['date'] = pd.to_datetime(df['date']) df['year'] = df['date'].dt.year df['month'] = df['date'].dt.month df['week'] = df['date'].dt.week date year month week 0 2019-10-12 2019 10 41 1 2019-10-20 2019 10 42 或者,这是相同的 df.assign(year = df['date'].dt.year, month=df['date'].dt.month, week=df['date'].dt.week)
如果列不是日期时间类型,则为第一行
或者,这是相同的