<p>尝试将<code>pd.Series</code>与<code>np.repeat</code>、<code>np.arange</code>、<code>pd.DataFrame.groupby</code>、<code>pd.Series.astype</code>、<code>pd.Series.map</code>和<code>pd.Series.fillna</code>一起使用:</p>
<pre><code>x = df.groupby('total_usage')
s = pd.Series(np.repeat(np.arange(len(x.ngroups), [len(i) for i in x.groups.values()]) + 1)
df['user_group'] = (s.astype(str) + s.map({1: 'st', 2: 'nd'}).fillna('th') + '_Group').values[::-1]
</code></pre>
<p>现在:</p>
<pre><code>print(df)
</code></pre>
<p>是:</p>
<pre><code> user_id total_usage user_group
0 1 10 10th_Group
1 2 10 10th_Group
2 3 20 9th_Group
3 4 20 9th_Group
4 5 30 8th_Group
5 6 30 8th_Group
6 7 40 7th_Group
7 8 40 7th_Group
8 9 50 6th_Group
9 10 50 6th_Group
10 11 60 5th_Group
11 12 60 5th_Group
12 13 70 4th_Group
13 14 70 4th_Group
14 15 80 3th_Group
15 16 80 3th_Group
16 17 90 2nd_Group
17 18 90 2nd_Group
18 19 100 1st_Group
19 20 100 1st_Group
</code></pre>