【问题标题】:change Pandas column object:time into list of string将 Pandas 列对象:时间更改为字符串列表
【发布时间】:2021-04-01 10:40:25
【问题描述】:

data = df['time_position'] 

time_position #input data 
01:22:00
00:22:00
00:00:01
02:22:00
03:22:00
00:22:00

dtype: object

如何将对象时间更改为 'str' ?

我希望将对象更改为 str ,然后使用以下命令将其更改为列表:

df['time_position'].tolist()

谢谢!提前

【问题讨论】:

  • str 在 pandas 中的类型也存储为 object 类型。

标签: python pandas string date time


【解决方案1】:

您可以简单地使用.astype(str)。因此:

df['time_position'] = df['time_position'].astype(str)

【讨论】:

    【解决方案2】:

    试试(如果你只需要这个):

    df['time_position'] = df['time_position'].str.split(":")
    

    time_position
    0   [01, 22, 00]
    1   [00, 22, 00]
    2   [00, 00, 01]
    3   [02, 22, 00]
    4   [03, 22, 00]
    5   [00, 22, 00]
    

    如果你想转换成 str 则使用.astype(str)

    【讨论】:

      猜你喜欢
      • 2015-04-24
      • 1970-01-01
      • 2015-10-24
      • 2017-04-08
      • 2022-09-23
      • 2020-08-27
      • 2015-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多