Home >Backend Development >Python Tutorial >How to Replace NaN Values in a Pandas DataFrame Column?

How to Replace NaN Values in a Pandas DataFrame Column?

Barbara Streisand
Barbara StreisandOriginal
2024-12-09 11:24:06633browse

How to Replace NaN Values in a Pandas DataFrame Column?

Replacing NaN Values in a DataFrame Column

When working with pandas DataFrames, it is common to encounter missing values represented as NaN (Not a Number). Handling these values is crucial to ensure accurate data analysis and prevent errors. This article provides a comprehensive guide on how to replace NaN values in a DataFrame column.

Background

The following DataFrame contains a column named "Amount" with some NaN values:

       Date                  Amount
67    2012-09-30 00:00:00   65211
68    2012-09-09 00:00:00   29424
69    2012-09-16 00:00:00   29877
70    2012-09-23 00:00:00   30990
71    2012-09-30 00:00:00   61303
72    2012-09-09 00:00:00   71781
73    2012-09-16 00:00:00     NaN
74    2012-09-23 00:00:00   11072
75    2012-09-30 00:00:00  113702
76    2012-09-09 00:00:00   64731
77    2012-09-16 00:00:00     NaN

Using DataFrame.fillna() or Series.fillna()

The most straightforward method to replace NaN values is using the fillna() method. It allows you to specify a value or a function to fill the missing data:

df['Amount'] = df['Amount'].fillna(0)

This will replace all NaN values in the "Amount" column with 0.

Filling NaN Values with Specific Values

To fill NaN values with specific values, use:

df['Amount'].fillna({NaN: 100})

This will replace NaN values with 100.

Filling NaN Values Based on Other Columns

You can also fill NaN values based on values in other columns:

df['Amount'].fillna(df['Amount'].mean())

This will fill NaN values with the mean value of the "Amount" column.

The above is the detailed content of How to Replace NaN Values in a Pandas DataFrame Column?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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