vicky
vicky

Reputation: 279

Filtering and filling Nan with mode in Pandas

I have many qualitative variables in one column1 and corresponding qualitative values in another column which have blank values. I want to replace the blanks with the MODE value of each qualitative variable in column 1. For example if I have varibles such as Accountant, Engineer, Manager, etc. in column 1 and if I have Bachelor's degree as MODE for accountant, Master's for Engineer in column 2, I want to replace the coressponding blanks with Bachelor's and Master's Correctly. How can I achieve this in pandas?

Upvotes: 1

Views: 105

Answers (1)

Vinoth96
Vinoth96

Reputation: 353

df.loc[df["columnname"]==0,"columnname"]=np.nan

df["columnname"]=df.groupby("groupbycolumnname").columnname.transform(lambda x: x.fillna(x.mode()))

Upvotes: 2

Related Questions