代码之家  ›  专栏  ›  技术社区  ›  ValientProcess

将pandas dataframe中的字符串更改为datetime

  •  0
  • ValientProcess  · 技术社区  · 7 年前

    我有一个包含以下列的数据帧:

           0           Pressure [bar] Temperature [C]   
    0   13:07:46.380:   30.0911         11.8    
    1   13:07:47.332:   30.0940         11.8    
    2   13:07:50.998:   30.0911         11.8
    

    我想将第一列转换为正确的日期时间,因此我尝试了:

    df2['Time'] = pd.to_datetime(df2[0],format='%H:%M:%S.%f:')
    

        0           Pressure [bar]  Temperature [C] Time
    0   13:07:46.380:   30.0911     11.8      1900-01-01 13:07:46.380
    1   13:07:47.332:   30.0940     11.8      1900-01-01 13:07:47.332
    2   13:07:50.998:   30.0911     11.8      1900-01-01 13:07:50.998
    

    这显然不是我想要的,我希望它只是时间,意思是:

     Pressure [bar] Temperature [C]     Time
    0       30.0911     11.8      13:07:46.380
    1       30.0940     11.8      13:07:47.332
    2       30.0911     11.8      13:07:50.998
    

    并使用时间列作为索引。

    有什么想法吗?

    谢谢您!

    1 回复  |  直到 7 年前
        1
  •  2
  •   ValientProcess    7 年前

    你试过:

      df2['Time'] = pd.to_datetime(df2['Time']).dt.time