I have a df (Apple_farm
) and need to calculate a percentage based off values found in two of the columns (Good_apples
and Total_apples
) and then add the resulting values to a new column within Apple_farm called 'Perc_Good'.
I have tried:
Apple_farm['Perc_Good'] = (Apple_farm['Good_apples'] / Apple_farm['Total_apples']) *100
However this results in this error:
TypeError: unsupported operand type(s) for /: 'str' and 'str'
Doing
Print Apple_farm['Good_apples']
and Print Apple_farm['Total_apples']
Yields a list with numerical values however dividing them seems to result in them being converted to strings?
I have also tried to define a new function:
def percentage(amount, total):
percent = amount/total*100
return percent
but are unsure on how to use this.
Any help would be appreciated as I am fairly new to Python and pandas!