Home Backend Development Python Tutorial How do you replace NaN values in a Pandas DataFrame with column averages using the `fillna` method?

How do you replace NaN values in a Pandas DataFrame with column averages using the `fillna` method?

Oct 29, 2024 pm 06:11 PM

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>
Copy after login

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>
Copy after login

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
Copy after login

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

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 in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

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...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

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 by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

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 without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

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...

How to dynamically create an object through a string and call its methods in Python? How to dynamically create an object through a string and call its methods in Python? Apr 01, 2025 pm 11:18 PM

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...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

See all articles