Answers for "pandas replace % with calculated"

0

pandas replace % with calculated

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
Posted by: Guest on September-02-2021

Code answers related to "pandas replace % with calculated"

Python Answers by Framework

Browse Popular Code Answers by Language