擅长:python、mysql、java
<p>使用<a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.crosstab.html" rel="nofollow noreferrer">^{<cd1>}</a>和<a href="http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.plot.bar.html" rel="nofollow noreferrer">^{<cd2>}</a>以及<code>stacked=True</code>参数:</p>
<pre><code>df = pd.DataFrame({
'Category':list('ffofofoftoof'),
'State':list('kkcffccccccc')
})
print (df)
Category State
0 f k
1 f k
2 o c
3 f f
4 o f
5 f c
6 o c
7 f c
8 t c
9 o c
10 o c
11 f c
df1 = pd.crosstab(df['State'], df['Category'])
print (df1)
Category f o t
State
c 3 4 1
f 1 1 0
k 2 0 0
df1.plot.bar(stacked=True)
</code></pre>
<p><a href="https://i.stack.imgur.com/7f5bo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7f5bo.png" alt="g"/></a></p>