【问题标题】:How can I unify a column with different time formats into one single format?如何将具有不同时间格式的列统一为一种格式?
【发布时间】:2021-01-20 03:34:01
【问题描述】:

我想将“日期”列中的所有值都放在相同的时间格式中(例如:25/11/2020)。

我试过了

matches['Date'] = matches['Date'].apply(lambda x: pd.to_datetime(x).strftime('%d/%m/%Y')[0])

得到了结果:

我已经在 stackoverflow 中看到了这种风格的不同主题,但它们似乎都不适合我的情况。有任何想法吗?提前致谢

【问题讨论】:

    标签: pandas dataframe date formatting time-series


    【解决方案1】:

    我相信你需要to_datetimedayfirst=True

    matches['Date'] = pd.to_datetime(matches['Date'], dayfirst=True).dt.strftime('%d/%m/%Y')
    

    【讨论】:

    • 这是很棒的代码,感谢分享,请你告诉我如何练习这么多来记住这些选项:) 任何帮助/指导将不胜感激先生,谢谢。
    • @RavinderSingh13 - 如果想准确指定格式,可以使用this
    猜你喜欢
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-30
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多