Pandas groupby with mode function

Viewed 103

I'm trying to solve a particular problem, and I got stuck in this case.

data.groupby(["department","region", "gender"])["age"].transform(lambda x: x.mode())

Why all values are changed to nan?

1 Answers

mode returns a Series containing the mode (modes if there are two), not actually a scalar value (e.g. an int). So get the first value using .iloc[0]:

data.groupby(["department","region", "gender"])["age"].transform(lambda x: x.mode().iloc[0])
Related