调查结果的条形图为 pd.value_counts()
Barplot of survey results as pd.value_counts()
我进行了一项调查,答案可以是 1-7,例如"absolutely unhappy" 到 "absolutely happy" 以及介于两者之间的所有数据,数据是一个 pandas 系列。对其执行 data.value_counts() 会产生有序的 table
5.0 6
6.0 5
7.0 5
3.0 1
2.0 1
我怎样才能将它转换成条形图,其中 a) 有 7 个条形图,每个条形图对应一个答案可能性,b) 排序 1-7 而不是根据大小和 c) 带有个人名称(非常不高兴) ,不快乐,部分不快乐,中性,部分快乐,快乐,非常快乐)而不是 1-7 的酒吧?谢谢!
通过zip
创建字典,通过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()
我进行了一项调查,答案可以是 1-7,例如"absolutely unhappy" 到 "absolutely happy" 以及介于两者之间的所有数据,数据是一个 pandas 系列。对其执行 data.value_counts() 会产生有序的 table
5.0 6
6.0 5
7.0 5
3.0 1
2.0 1
我怎样才能将它转换成条形图,其中 a) 有 7 个条形图,每个条形图对应一个答案可能性,b) 排序 1-7 而不是根据大小和 c) 带有个人名称(非常不高兴) ,不快乐,部分不快乐,中性,部分快乐,快乐,非常快乐)而不是 1-7 的酒吧?谢谢!
通过zip
创建字典,通过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()