how to fill missing values dataframe with mean
To fill missing values in a dataframe with the mean, you can use the fillna()
method in Python. Here's an example of how to do it:
import pandas as pd
# Create a sample dataframe
data = {'A': [1, 2, None, 4, 5],
'B': [None, 2, 3, None, 5],
'C': [1, 2, 3, 4, None]}
df = pd.DataFrame(data)
# Fill missing values with the mean
df_filled = df.fillna(df.mean())
# Print the filled dataframe
print(df_filled)
This will replace all the missing values in the dataframe with the mean value of each column. The resulting dataframe, df_filled
, will have the missing values filled with the mean.
Please note that this example assumes you have already imported the pandas library and created a dataframe named df
with missing values. You can replace df
with your actual dataframe name in the code.