I have a simple initial dataframe:
ID, ATTRIBUTE
1, thing2
1, thing3
1, thing3
2, thing7
2, thing7
2, thing2
3, thing1
3, thing2
I have a simple groupby object I want to create where I get the mode of ATTRIBUTE (if it is multimodal I call the result 'multithing'):
mode = lambda x: x.mode() if len(x) > 2 else 'multithing'
df_grouped = df.groupby(['ID'], as_index=False)['ATTRIBUTE].agg(mode)
I am trying to get a result after reindexing that looks like this:
ID, ATTRIBUTE
1, thing3
2, thing7
3, multithing
So I can use it like a regular dataframe again and do things like this:
df_final.groupby('ATTRIBUTE')['ID'].count()