O'Reilly logo

Learning pandas - Second Edition by Michael Heydt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Filling missing values with the mean of the group

A common transformation in statistical analysis with grouped data is to replace missing data within each group with the mean of the non-NaN values in the group. To demonstrate this, the following code creates a DataFrame with a Label column with two values (A and B) and a Values column containing a series of integers, but with one value replaced with NaN. The data is then grouped by the Label column:

The mean of each group can be calculated using .mean():

Now suppose that we need the B group ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required