Convert your series to categorical, extract categories whose counts are not in the top 3, add a new category e.g. 'Other'
, then replace the previously calculated categories:
df['Jobrol'] = df['Jobrol'].astype('category')
others = df['Jobrol'].value_counts().index[3:]
label = 'Other'
df['Jobrol'] = df['Jobrol'].cat.add_categories([label])
df['Jobrol'] = df['Jobrol'].replace(others, label)
Note: It's tempting to combine categories by renaming them via df['Jobrol'].cat.rename_categories(dict.fromkeys(others, label))
, but this won't work as this will imply multiple identically labeled categories, which isn't possible.
The above solution can be adapted to filter by count. For example, to include only categories with a count of 1 you can define others
as so:
counts = df['Jobrol'].value_counts()
others = counts[counts == 1].index
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…