I need to "simplify" for reporting purposes rare events in a pandas DataFrame resulting from a group-by operation.
Let's take for example this DataFrame, where I use colA to count the occurrences of values in colB
df = pd.DataFrame(data={'colA':['a','b','c','d','e','a','a','b','b','a'],'colB':[1,2,3,4,5,1,1,2,2,1]})
df_grouped = df.groupby(['colA']).agg('count')
The result is:
colB
colA
a 4
b 3
c 1
d 1
e 1
from this grouped dataframe I want to obtain a new dataframe, where the least frequent values, namely those corresponding to colA={'c','d','e'} are grouped in a new value of the colA level called 'other', that contains the total number of all these, like the following:
colB
colA
a 4
b 3
other 3
Is there a simple way to perform this "put-rare-stuff-in-the-trashbin" operation? Moreover, how can I do it in the presence of a MultiIndex?