I found an interesting library and I would like to make changes in this code. I would like to see the number of events divided by the sum of events (like 5/52 etc.), under percentages. How can I add it?
link to the library: https://mplsoccer.readthedocs.io/en/latest/gallery/plots/plot_heatmap_positional.html
This part of the code displays data but I don’t know how to change it:
total = np.array([bs['statistic'].sum() for bs in bin_statistic]).sum() # replace raw counts with percentages and add percentage sign (note immutable named tuple so used _replace) for bs in bin_statistic: bs['statistic'] = (pd.DataFrame(bs['statistic'] / total) .applymap(lambda x: '{:.0%}'.format(x)) .values)
Advertisement
Answer
The code you have shown replaces the "statistic"
column of the dataframe with a new column, which contains the percentages.
bs['statistic'] = (pd.DataFrame(bs['statistic'] / total) .applymap(lambda x: '{:.0%}'.format(x)) .values)
Let’s split it into a couplelines to make it easier to understand:
# divide the statistic column by the total percentages = pd.DataFrame(bs['statistic'] / total) # format percentages using the specifier {:.0%} bs['statistic'] = (percentages.applymap(lambda x: '{:.0%}'.format(x)).values)
Once the percentages are calculated, the str.format()
function is used to format each value as a percentage with zero decimal places (https://www.w3schools.com/python/ref_string_format.asp)
So all you need to do is change what is being formatted, and how it is formatted.
outputval = pd.DataFrame(bs['statistic']) # it might not be necessary to cast to pd.DataFrame bs['statistic'] = (outputval.applymap(lambda x: '{0:.0f}/{1:.0f}'.format(x, total)).values)
Or in one line:
bs['statistic'] = (pd.DataFrame(bs['statistic']).applymap(lambda x: '{0:.0f}/{1:.0f}'.format(x, total)).values)