python - Barpots of item categories in pandas -


i'm new pandas , not sure how draw bar plot of each category (6, 7) showing proportion (percentage) of "wet" other states in following data frame:

mine    category    state x23 6   wet m34 7   wet k28 7   dry u56 7   swampy s90 6   wet e35 7   dry x67 6   wet 

my effort far:

sub =df.groupby(['category','state==wet'].sum() sub.plot(kind='bar') 

can please? thanks

edit: dataframe output plotted

category    percent “wet”   percent “non wet” 6   3/3 (100%)  0/3 (0%) 7   1/4 ¾ ( 75 %) 

so 6 , 7 on x-axis, wet , non-wet stacked in each of bar(6) , bar(7).

you can use custom function f1 dataframe constructor , t:

def f1(x):     return (sum(x == 'wet') / float(len(x)))*100, (sum(x != 'wet') / float(len(x)))*100  grouped = df.groupby(['category'])['state'].apply(f1)  new_cols = ['wet','non-wet'] print pd.dataframe(zip(*grouped), columns=grouped.index,index=new_cols).t             wet  non-wet category                 6         100.0      0.0 7          25.0     75.0  sub.plot(kind='bar') 

Comments