0

I have a Pandas dataframe with about 10 binary variables and I want to plot the zero and one counts in a stacked barchart using Seaborn. Anyone can help me how to do this?

ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
chemook78
  • 1,168
  • 3
  • 17
  • 38

1 Answers1

2

I think is possible create stacked bar in seaborn, but really complicated.

Simplier is use DataFrame.plot.bar with parameter stacked=True:

from collections import Counter

df = pd.DataFrame({'A':['1110','1111', '0000']})

print (df)
      A
0  1110
1  1111
2  0000

#get counts of 0, 1
x = Counter([a for x in df['A'] for a in list(x)])
print (x)
Counter({'1': 7, '0': 5})

df = pd.DataFrame([x])
print (df)
   0  1
0  5  7

df.plot.bar(stacked=True)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252