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?
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?
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])