<p>使用<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.map.html" rel="nofollow noreferrer">pandas.Series.map</a>:</p>
<pre><code>df['Frecuency']=df['Category'].map(df['Category'].value_counts())
</code></pre>
<p>或<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.replace.html" rel="nofollow noreferrer">pandas.Series.replace</a>:</p>
<pre><code>df['Frecuency']=df['Category'].replace(df['Category'].value_counts())
</code></pre>
<p><strong>输出:</strong></p>
<pre><code> Index Category Frecuency
0 0 1 1
1 1 3 2
2 2 3 2
3 3 4 1
4 4 7 3
5 5 7 3
6 6 7 3
7 7 8 1
</code></pre>
<p><strong>细节</strong></p>
<pre><code>df['Category'].value_counts()
7 3
3 2
4 1
1 1
8 1
Name: Category, dtype: int64
</code></pre>
<p>使用<code>value_counts</code>可以得到一个序列,它的<code>index</code>是类别的元素,<code>values</code>是计数。因此,可以使用<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.map.html" rel="nofollow noreferrer">map</a>或<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.replace.html" rel="nofollow noreferrer">pandas.Series.replace</a>创建一个系列,其中<code>category</code>值替换为计数中的值。最后将这个序列赋给<code>frequency</code>列</p>