Home >Backend Development >Python Tutorial >How to Replace NaN Values in a Pandas 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.
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
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.
To fill NaN values with specific values, use:
df['Amount'].fillna({NaN: 100})
This will replace NaN values with 100.
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!