简体   繁体   中英

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)

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.

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