I am trying to calculate the percentage of occurrences of a product (with hundreds of different products) according to the related Country. And I would like this % to be shown on another column
I managed to do it in a graph, but it is not visually pleasing.
I tried the following code:
df = data1.groupby('Country')
df['percent'] = (df['Products'] /
df['Products'].value_counts())*100
df
I get the following error message : "ValueError: operands could not be broadcast together with shapes (111,2) (4209,)"
I also tried something like the following by modifying it to fit my dataframe, but without success.
gb = df.groupby("country")
gb['result'].agg(lambda x: len(x[x=="Fail"]) / len(x)).sort_values(by="% fail", ascending=False)
To summarize I would like something that would look like this:
Countries Products Percentage
0 Country 1 Product 1 0.5
1 Country 1 Product 2 0.01
2 Country 2 Product 1 0.2
3 Country 2 Product 2 0.05
And so on.
Thank you in advance for your help!
我想你想要:
df['percent'] = df.groupby('Country')['Products'].value_counts(normalize=True) * 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.