【问题标题】:Convert full format date to year month format in Python [duplicate]在Python中将完整格式日期转换为年月格式[重复]
【发布时间】:2020-02-24 14:58:03
【问题描述】:

我的数据框有一列date,其中包含以下数据:

0         2019/5/20 22:49:29
1         2019/5/20 23:18:23
2           2019/3/8 9:11:35
3           2019/3/8 9:19:58
4         2019/5/20 22:57:12
5           2019/3/8 9:06:41

如何将其转换为格式year-month:

0         2019-05
1         2019-05
2         2019-03
3         2019-03
4         2019-05
5         2019-03

谢谢。

【问题讨论】:

    标签: python pandas


    【解决方案1】:

    有效:

    df['date'] = pd.to_datetime(df['date']).dt.to_period('M')
    

    【讨论】:

      猜你喜欢
      • 2015-04-29
      • 2021-04-12
      • 1970-01-01
      • 1970-01-01
      • 2021-06-15
      • 1970-01-01
      • 2021-12-12
      相关资源
      最近更新 更多