擅长:python、mysql、java
<p>您可以使用<code>.groupby()</code>和内置的<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.div.html" rel="noreferrer">^{<cd2>}</a>:</p>
<pre><code>rating_probs = df.groupby('rating').size().div(len(df))
rating
A 0.333333
B 0.500000
C 0.166667
</code></pre>
<p>还有条件问题:</p>
<pre><code>df.groupby(['type', 'rating']).size().div(len(df)).div(rating_probs, axis=0, level='rating')
coupe A 0.500000
B 0.333333
sedan A 0.500000
B 0.666667
C 1.000000
</code></pre>