


How do you replace NaN values in a Pandas DataFrame with column averages using the `fillna` method?
Replacing NaN Values with Column Averages in a Pandas DataFrame
When dealing with missing data in a DataFrame populated primarily with real numbers, replacing NaN values with appropriate alternatives is essential. In this case, we seek to replace NaN values with the averages of the columns where they reside.
To address this need, pandas provides a convenient method: DataFrame.fillna. By utilizing this function, we can directly fill NaN values with column averages:
<code class="python">df = ... # Your DataFrame with NaN values # Calculate the mean of each column column_means = df.mean() # Replace NaN values with the column averages filled_df = df.fillna(column_means)</code>
The DataFrame.fillna method accepts various inputs as the fill value, including a scalar, a dict, or a Series. In this instance, we pass column_means, a Series containing the mean of each column.
Here's an example to illustrate the process:
<code class="python">import pandas as pd df = pd.DataFrame({ 'A': [-0.166919, -0.297953, -0.120211, NaN, NaN, -0.788073, -0.916080, -0.887858, 1.948430, 0.019698], 'B': [0.979728, -0.912674, -0.540679, -2.027325, NaN, NaN, -0.612343, 1.033826, 1.025011, -0.795876], 'C': [-0.632955, -1.365463, -0.680481, 1.533582, 0.461821, NaN, NaN, NaN, -2.982224, -0.046431] }) print(df) # Calculate the mean of each column column_means = df.mean() # Replace NaN values with the column averages filled_df = df.fillna(column_means) print(filled_df)</code>
Output:
A B C 0 -0.166919 0.979728 -0.632955 1 -0.297953 -0.912674 -1.365463 2 -0.120211 -0.540679 -0.680481 3 NaN -2.027325 1.533582 4 NaN NaN 0.461821 5 -0.788073 NaN NaN 6 -0.916080 -0.612343 NaN 7 -0.887858 1.033826 NaN 8 1.948430 1.025011 -2.982224 9 0.019698 -0.795876 -0.046431 A B C 0 -0.166919 0.979728 -0.632955 1 -0.297953 -0.912674 -1.365463 2 -0.120211 -0.540679 -0.680481 3 -0.151121 -2.027325 1.533582 4 -0.151121 -0.231291 0.461821 5 -0.788073 -0.231291 -0.530307 6 -0.916080 -0.612343 -0.530307 7 -0.887858 1.033826 -0.530307 8 1.948430 1.025011 -2.982224 9 0.019698 -0.795876 -0.046431
As illustrated, the NaN values have been replaced with the appropriate column averages, providing a complete and consistent DataFrame.
The above is the detailed content of How do you replace NaN values in a Pandas DataFrame with column averages using the `fillna` method?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...

Using python in Linux terminal...

Fastapi ...
