Home >Backend Development >Python Tutorial >How to Fill Missing Values in Pandas with the Group Mean?
Filling Missing Values by Mean in Each Group in Pandas
In this context, your objective is to fill in missing values ("NaN") with the mean value for each distinct group within the "name" column. To achieve this, you can utilize Pandas' transform function in conjunction with the fillna method.
Let's consider the example DataFrame provided:
To compute the mean value within each "name" group, you can employ the groupby function:
However, grouped is a DataFrame containing the mean values, and we need to apply it back to the original DataFrame to fill in the missing values. This is where transform becomes useful:
Here's a breakdown of the code:
lambda x: x.fillna(x.mean()) defines an anonymous function that takes each group (represented by x) as input and performs the following operation:
After executing the code, the resulting DataFrame will contain the filled-in values as desired:
The above is the detailed content of How to Fill Missing Values in Pandas with the Group Mean?. For more information, please follow other related articles on the PHP Chinese website!