【问题标题】:Replacing + sign with sum of those 2 numbers in Pandas dataframe用 Pandas 数据框中这两个数字的总和替换 + 符号
【发布时间】:2021-04-30 08:32:19
【问题描述】:

我在名为 price 的数据框中有一列,其值为 100、200、2300、2300+200 等。 如何将 2300+200 替换为这两个数字的总和,即 2500。

【问题讨论】:

    标签: python-3.x pandas string integer


    【解决方案1】:

    apply 中使用eval

    df = pd.DataFrame({'col':[10,'1000+1']})
    
    df['col'] = df['col'].apply(pd.eval)
    print (df)
        col
    0    10
    1  1001
    

    【讨论】:

      猜你喜欢
      • 2022-09-23
      • 2013-06-11
      • 1970-01-01
      • 2021-05-13
      • 2018-08-11
      • 2012-12-30
      • 2018-07-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多