1

I have conducted a survey where answers can be 1-7, as in e.g. "absolutely unhappy" to "absolutely happy" and everything in between, the data is a pandas series. Conducting data.value_counts() on it yields the ordered table

5.0  6
6.0  5
7.0  5
3.0  1
2.0  1

how can I convert it to a bar plot, where a) 7 bars are present, one for each answer possibility, b) ordered 1-7 instead of according to the magnitude and c) with individual names (extremely unhappy, unhappy, partly unhappy, neutral, partly happy, happy, extremely happy) instead of 1-7 for the bars? Thanks!

sfluck
  • 345
  • 1
  • 2
  • 8

1 Answers1

1

Create dictionary by zip, map index by Index.map and reindex for add missing catogories with set ordering, last plot by Series.plot.bar:

s = pd.Series([6,5,5,1,1], index=[5.0,6.0,7.0,3.0,2.0])

cats = ['extremely unhappy', 'unhappy', 'partly unhappy', 
        'neutral', 'partly happy', 'happy', 'extremely happy']
vals = range(1, 8)
d = dict(zip(vals, cats))

s.index = s.index.map(d.get)
s1 = s.reindex(cats, fill_value=0)
print (s1)
extremely unhappy    0
unhappy              1
partly unhappy       1
neutral              0
partly happy         6
happy                5
extremely happy      5
dtype: int64

s1.plot.bar()
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252