0

Suppose I have a data like

 df = pd.Dataframe({'id': [1,2,3,4,5], 'Disease' : [0,0,1,0,1], 'code':['A', 'B', 'B', 'A', 'A']})

Now I know that if I groupby code

m = df.groupby(df.code).size()

I have the frequency chart of people with code A and B. I need to plot this as a bar graph but I want the percentage graph. That is y axis is percentage from 0 to 100 and x-axis to be code. I want see what percent of people with code A has status 0 and 1, similarly the other way round. As someone dealt with this before ?

1 Answer 1

1

How about this?

df.groupby('code').mean()['Disease'].plot(kind='bar')

It will be a fraction, not percentage, but I'm sure you can take it form here.

Sign up to request clarification or add additional context in comments.

3 Comments

this is a more elegant version: df['Disease'].groupby(df.code).mean().plot(kind='bar')
I want stacked in this with showing status 0 and 1 in different colors
Use the 'color' variable of DataFrame.plot , like this: df['Disease'].groupby(df.code).mean().plot(kind='bar', color=['red', 'green'])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.