代码之家  ›  专栏  ›  技术社区  ›  8-Bit Borges

Pandas-将值作为类型字符串转换为float

  •  1
  • 8-Bit Borges  · 技术社区  · 4 年前

    dtype(str)

          x
      27:47
      13:45
      10:45
    

    我想把它们转换成 float ,最后是:

          x
      27.47
      13.45
      10.45
    

    我该怎么做?

    2 回复  |  直到 4 年前
        1
  •  1
  •   Quang Hoang    4 年前

    在您的情况下,您可以:

    df['x'] = df['x'].str.replace(':','.').astype(float)
    

    输出:

           x
    0  27.47
    1  13.45
    2  10.45
    
        2
  •  1
  •   Wasif    4 年前

    df['x'] = df['x'].replace(to_replace=":",value=".").astype(float)