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!