【问题标题】:loop through a list and convert a column to datatime循环遍历列表并将列转换为日期时间
【发布时间】:2019-07-17 01:44:05
【问题描述】:

我有一个包含数据框的列表。我想遍历列表中的每个数据框,并为每个数据框选择“时间”列并将其转换为日期时间对象。这是我写的代码,但它给出了错误“列表索引必须是整数或切片,而不是 DataFrame”

for i in list_of_dataframes:
  list_of_dataframes[i].to_datetime(list_of_dataframes[i].eventTime)

【问题讨论】:

  • i.eventTime?虽然看起来也很奇怪。你能提供一个MVCE吗?

标签: python pandas loops datetime


【解决方案1】:

你的问题就在前面:

for i in list_of_dataframes:
  list_of_dataframes[i] ...

i 是一个数据框,就像你问的那样。您为什么要尝试将其用作数据框列表的索引?试试这个吧:

for df in list_of_dataframes:
  df.to_datetime(df.eventTime)

由于您未能提供所需的完整示例,我需要问:您的数据框类是否甚至 to_datetime 方法?如果没有,您需要更正该调用以使用确实具有您需要的方法的对象。

【讨论】:

  • 你是对的!我收到一个错误:“'DataFrame' 对象没有属性 'to_datetime'”。我的列是“id”、“eventTime”、“value”。
猜你喜欢
  • 2018-10-01
  • 2019-08-20
  • 2017-05-14
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-18
  • 2013-06-22
相关资源
最近更新 更多