Home > Backend Development > Python Tutorial > How to Fill Missing Values in Pandas with the Group Mean?

How to Fill Missing Values in Pandas with the Group Mean?

Linda Hamilton
Release: 2024-12-11 12:58:11
Original
516 people have browsed it

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:

  • df.groupby("name") groups the DataFrame by the "name" column.
  • lambda x: x.fillna(x.mean()) defines an anonymous function that takes each group (represented by x) as input and performs the following operation:

    • It fills in missing values ("NaN") with the mean value calculated using x.mean().
  • df["value"] replaces the "value" column in the original DataFrame with the transformed values that fill in the missing values.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template