Home > Back-end >  pandas groupby count percent of positive category
pandas groupby count percent of positive category

Time:09-06

I have dataframe

df = pd.DataFrame({
    'state': ['CA', 'WA', 'CO', 'AZ'] * 3,
    'year': [np.random.randint(2015, 2020) for _ in range(12)],
    'sales': [np.random.randint(-100, 100) for _ in range(12)]
})

i want to count for each state, what percent of year observed has positive sales. My current code is

df.groupby(['state', 'year']).agg({'sales': 'sum'})

intermediate df

The desired output would be a dataframe such as CA: 100%, WA, 50%, AZ:67% etc

CodePudding user response:

There are probably more elegant way to do it, and I would first create a column with a 1 if sale is positive and 0 if negative. Then I would aggregate on that new column using a mean aggregate function. Then finally multiply by 100.

  • Related