1

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()

1 Answer 1

2

IIUC:

In [203]: df.groupby('ID')['ATTRIBUTE'] \
            .agg(lambda x: x.mode()[0] if len(x.mode()) == 1 else 'multithing')
Out[203]:
ID
1        thing3
2        thing7
3    multithing
Name: ATTRIBUTE, dtype: object

or

In [205]: df.groupby('ID', as_index=False)['ATTRIBUTE'] \
     ...:   .agg(lambda x: x.mode()[0] if len(x.mode()) == 1 else 'multithing')
Out[205]:
   ID   ATTRIBUTE
0   1      thing3
1   2      thing7
2   3  multithing
Sign up to request clarification or add additional context in comments.

2 Comments

Second is perfect. I see what I had been doing wrong with the lambda function previously.
One last question if you don't mind? Does the mode lambda in this case ignore np.nan values for ATTRIBUTE? I would want it to only find the mode of the non-null values.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.