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'})
The desired output would be a dataframe such as CA: 100%, WA, 50%, AZ:67% etc
