📜  pandas 用计算替换 % - Python 代码示例

📅  最后修改于: 2022-03-11 14:46:51.051000             🧑  作者: Mango

代码示例1
def FormatPerc(x): # Create the function to be applied
    perc = 0
    x = str(x)
    
    if '%' in x:
        x = x.replace("%","")
        perc = float(x)*0.001
    else:
        x = x.replace("%","")
        perc = float(x)   
    
    return perc
  
# in your pandas script:
investments['WithdrawPercentage'] = investments['WithdrawPercentage'].apply(FormatPerc)
# any values like "xx.x%" will now be formatted as 0.xxx