How to calculate percentage of row value in sum of total values in that column in python?

Question:

I have data frame containing red and blue balloons and I want to see what is the percentage of either balloons using Python:

Current:
Balloons Count
Red 10
Blue 90

Wanted:
Balloons Count Percentage
Red 10 10%
Blue 90 90%

Answers:

maybe

red_percent = 100 * red / (red + blue)
blue_percent = 100 * blue / (red + blue)
 

or:

red_percent = 100 * red / (red + blue)
blue_percent = 100 - red_percent 
Answered By: Zhihar

if you have the numbers of occurencies you can do that with just a little of math

red_ballons = 10
blue_ballons = 90

total_ballons = red_ballons + blue_ballons

red_percentage = red_ballons  / total_ballons  * 100 #*100 because %
blue_percentage = blue_ballons  / total_ballons  * 100 #*100 because %

Im not sure if this is what you were looking for but i hope it helps

Answered By: Sheyteo

Resolved:

data['Percentage'] = (data['Count']/data['Count'].sum()) *100
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.