I am facing a little challenge to create a certain logic in python, I am creating a dataset that has three lists, I want to calculate the percentage of every i-th value of the list and create new lists, here is the example of what I am trying to achieve
I have these three lists
good=[500,400,300]
opportunity=[300,200,100]
bad=[100,50,20]
I am able to have the list of total of the i-th values
total=[x + y + z for x, y, z in zip(good, opportunity ,bad)]
##total=[900,650,420]
now I want the list to be converted into percentages as
good=[(500/900)*100,(400/900)*100,(300/900)*100]
opportunity=[(300/650)*100,(200/650)*100,(100/650)*100]
bad=[(100/420)*100,(50/420)*100,(20/420)*100]
good = [(x/total[0])*100 for x in good]
opportunity=[(x/total[1])*100 for x in opportunity]
bad=[(x/total[2])*100 for x in bad]
output:
[55.55555555555556, 44.44444444444444, 33.33333333333333]
[46.15384615384615, 30.76923076923077, 15.384615384615385]
[23.809523809523807, 11.904761904761903, 4.761904761904762]
You can use a list of lists, and numpy to make this problem easier to solve
import numpy as np
#List of lists from good, opportunity and bad
li = [[500,400,300],[300,200,100],[100,50,20]]
#Convert list to numpy arr
arr = np.array(li)
#Calculate total
total=[x + y + z for x, y, z in zip(*li)]
#Use numpy.divide to divide each element of list by total
print([np.divide(li[idx], total[idx]) for idx in range(3)])
The output will then be
[array([0.55555556, 0.44444444, 0.33333333]),
array([0.46153846, 0.30769231, 0.15384615]),
array([0.23809524, 0.11904762, 0.04761905])]
You can also assign back your percentages to variables as follows
perc_good, perc_opportunity, perc_bad = [np.divide(li[idx], total[idx]) for idx in range(3)]
print(list(perc_good))
print(list(perc_opportunity))
print(list(perc_bad))
The output will be
[0.5555555555555556, 0.4444444444444444, 0.3333333333333333]
[0.46153846153846156, 0.3076923076923077, 0.15384615384615385]
[0.23809523809523808, 0.11904761904761904, 0.047619047619047616]
In your example, your divisors are computed on columns (ith positions) but are applied across rows instead of columns. I assume that you actually meant to apply them on columns (eg good=[(500/900)*100,(400/650)*100,(300/420)*100]
)
Once you have your list of totals you can use zip to transform each list of values into a list of percentages:
totals = [ sum(v) for v in zip(good,opportunity,bad) ]
goodPercent = [ 100*v/t for v,t in zip(good,totals) ]
opportunityPercent = [ 100*v/t for v,t in zip(opportunity,totals) ]
badPercent = [ 100*v/t for v,t in zip(bad,totals) ]
# goodPercent: [55.55555555555556, 61.53846153846154, 71.42857142857143]
# opportunityPercent: [33.333333333333336, 30.76923076923077, 23.80952380952381]
# badPercent: [11.11111111111111, 7.6923076923076925, 4.761904761904762]
# (columns add up to 100%)
On the other hand, if you want percentages across rows, then you should treat each list individually:
goodPercent = [ 100*v/t for t in [sum(good)] for v in good ]
opportunityPercent = [ 100*v/t for t in [sum(opportunity)] for v in opportunity ]
badPercent = [ 100*v/t for t in [sum(bad)] for v in bad ]
# goodPercent: [41.666666666666664, 33.333333333333336, 25.0]
# opportunityPercent: [50.0, 33.333333333333336, 16.666666666666668]
# badPercent: [58.8235294117647, 29.41176470588235, 11.764705882352942]
# (rows add up to 100%)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.