简体   繁体   English

在 python pandas 中使用 groupby 的列中出现的百分比

[英]Percentage of occurrences in a column using groupby in python pandas

[enter image description here] [在此处输入图像描述]

1

Suppose I have multiple entries in a column search term, I want to calculate the percentage of occurrence of the brand.假设我在一个列搜索词中有多个条目,我想计算品牌出现的百分比。 I know how to get the count of each brand but can someone suggest a way to get these in percentage?我知道如何获得每个品牌的数量,但有人可以建议一种方法来获得这些百分比吗?

df = df.groupby(["searchterm","brand"]).size().reset_index(name='count')

You should be able to use你应该可以使用

df.groupby('searchterm')['brand'].value_counts(normalize=True)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM