Home > OS >  Percentage of occurrences in a column using groupby in python pandas
Percentage of occurrences in a column using groupby in python pandas

Time:12-23

[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')

CodePudding user response:

You should be able to use

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