【问题标题】:How to convert a Python Pandas DateTime Object to a string如何将 Python Pandas DateTime 对象转换为字符串
【发布时间】:2021-12-07 08:57:54
【问题描述】:

我有一个 Pandas DataFrame,其中包含日期时间格式的“报告日期”。 df 包含一条记录。我只想获取字符串值。这是我正在使用的:

report_date = df['Report Date'].dt.strftime('%Y-%m-%d')
report_date

产生这个输出:

0    2021-06-30

Name: Report Date, dtype: object

如何获得一个只是字符串“2021-06-30”的变量?

【问题讨论】:

标签: python pandas datetime


【解决方案1】:

添加以下行:

report_date = df['Report Date'].dt.strftime('%Y-%m-%d')
report_date = report_date[0]

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-12
  • 2011-09-21
  • 1970-01-01
  • 1970-01-01
  • 2016-11-13
  • 2021-03-31
  • 1970-01-01
相关资源
最近更新 更多