Calculating and creating percentage column from two columns
Asked Answered
M

2

8

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!

Medea answered 31/3, 2016 at 11:29 Comment(0)
T
11

I think you need convert string columns to float or int, because their type is string (but looks like numbers):

Apple_farm['Good_apples'] = Apple_farm['Good_apples'].astype(float)
Apple_farm['Total_apples'] = Apple_farm['Total_apples'].astype(float)

Apple_farm['Good_apples'] = Apple_farm['Good_apples'].astype(int)
Apple_farm['Total_apples'] = Apple_farm['Total_apples'].astype(int)

Sample:

import pandas as pd

Good_apples = ["10", "20", "3", "7", "9"]
Total_apples = ["20", "80", "30", "70", "90"]
d = {"Good_apples": Good_apples, "Total_apples": Total_apples}
Apple_farm = pd.DataFrame(d)
print Apple_farm 
  Good_apples Total_apples
0          10           20
1          20           80
2           3           30
3           7           70
4           9           90

print Apple_farm.dtypes
Good_apples     object
Total_apples    object
dtype: object

print Apple_farm.at[0,'Good_apples']
10

print type(Apple_farm.at[0,'Good_apples'])
<type 'str'>
Apple_farm['Good_apples'] = Apple_farm['Good_apples'].astype(int)
Apple_farm['Total_apples'] = Apple_farm['Total_apples'].astype(int)

print Apple_farm.dtypes
Good_apples     int32
Total_apples    int32
dtype: object

print Apple_farm.at[0,'Good_apples']
10

print type(Apple_farm.at[0,'Good_apples'])
<type 'numpy.int32'>
Apple_farm['Perc_Good'] = (Apple_farm['Good_apples'] / Apple_farm['Total_apples']) *100

print Apple_farm
   Good_apples  Total_apples  Perc_Good
0           10            20       50.0
1           20            80       25.0
2            3            30       10.0
3            7            70       10.0
4            9            90       10.0
Tachycardia answered 31/3, 2016 at 11:35 Comment(1)
That apple farm needs to find some better applesTwophase
R
0

This code may helps you :

revenue_per_countries = df.groupby(["Country"])["Amount"].sum().sort_values()

revenue_per_countries = pd.DataFrame(revenue_per_countries)

revenue_per_countries['percent'] = revenue_per_countries['Amount']/revenue_per_countries['Amount'].sum()*100

revenue_per_countries = revenue_per_countries.sort_values(by=['percent'], ascending=False)

revenue_per_countries = revenue_per_countries.head(15)

revenue_per_countries.head(15)
Retrorocket answered 21/7, 2022 at 11:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.