kim kevin
kim kevin

Reputation: 185

pandas fill missing value with median given group

I have a data set with 100+ columns and half million rows. For column Z, some of the values are missing. I am going to first group the data by 2 columns (A,B), then for each group, I get the median (skip those nans), then I want to fill the nans in Z by those median for the corresponding group.

I could

df.groupby(["A","B"]).Z.median()

But it also has nans in some of the group and I am not sure how to proceed to really fill those nans in Z by the group median...

Upvotes: 2

Views: 1544

Answers (1)

Abdou
Abdou

Reputation: 13274

Try:

df.groupby(["A","B"]).Z.apply(lambda x: x.fillna(x.median()))

Upvotes: 3

Related Questions