【问题标题】:How to format string without leading zeros and + - as integer in python pandas?python - 如何在没有前导零和+的情况下将字符串格式化为python pandas中的整数?
【发布时间】:2022-09-23 10:47:16
【问题描述】:

我在熊猫数据框中有一列带有字符串的行,如下所示:

+0094
-0082

我将如何替换列中的所有值,以便它们的格式如下:

 94
-82

    标签: python pandas jupyter-notebook


    【解决方案1】:

    pandas 足够聪明,可以使用数据框的 astype 方法来解决这个问题。

    所以你的方法是:

    df['col of interest'] = df['col of interest'].astype(int)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-12
      • 1970-01-01
      • 2016-02-12
      • 2012-04-30
      • 1970-01-01
      • 2020-08-04
      • 2011-05-02
      • 2022-07-07
      相关资源
      最近更新 更多