<p>您可以使用first <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.GroupBy.transform.html" rel="nofollow noreferrer">^{<cd1>}</a>with <code>size</code>first作为find frequency,然后使用<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.cut.html" rel="nofollow noreferrer">^{<cd3>}</a>作为create bins,最后使用<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.groupby.html" rel="nofollow noreferrer">^{<cd4>}</a>with <code>size</code>,通过<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.unstack.html" rel="nofollow noreferrer">^{<cd6>}</a>重塑形状并通过<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reindex.html" rel="nofollow noreferrer">^{<cd7>}</a>填充缺少的列:</p>
<pre><code>df['FREQ'] = df.groupby('ID_PERSON')['EVALUATION_GRADE'].transform('size')
bins = [-np.inf, 1, 3, np.inf]
labels=['1 time','2-3 times','> 3 times']
df.FREQ = pd.cut(df.FREQ, bins=bins, labels=labels)
df = df.groupby(['FREQ', 'EVALUATION_GRADE'])['EVALUATION_GRADE'] \
.size() \
.unstack(fill_value=0) \
.reindex(columns=np.arange(1,6), fill_value=0)
df.columns = 'GRADE ' + df.columns.astype(str)
print (df)
GRADE 1 GRADE 2 GRADE 3 GRADE 4 GRADE 5
FREQ
1 time 1 0 0 0 1
2-3 times 3 2 0 0 1
> 3 times 0 2 1 0 1
</code></pre>