In Python, you can replace missing values (NaN or null values) in a Pandas dataframe using the fillna() method. The fillna() method replaces any missing values with the specified value or method.
Here's an example of how to use the fillna() method to replace missing values in a Pandas dataframe:
import pandas as pd# create a sample dataframe with missing valuesdata = {'name': ['John', 'Jane', 'Mike', 'Susan'], 'age': [30, None, None, 40], 'gender': ['M', 'F', None, 'F']}df = pd.DataFrame(data)# replace missing values in the dataframe with the mean agemean_age = df['age'].mean()df_filled = df.fillna(value={'age': mean_age})print(df_filled) |
This will output a new dataframe with the missing values replaced:
name age gender0 John 30.0 M1 Jane 30.0 F2 Mike 30.0 F3 Susan 40.0 F |
In this example, the fillna() method has replaced the missing values in the age column with the mean age of the non-missing values (35).
You can also use other methods to replace missing values, such as forward fill (ffill) or backward fill (bfill):
# replace missing values in the dataframe using forward filldf_ffill = df.fillna(method='ffill')print(df_ffill) |
This will output a new dataframe with the missing values replaced using forward fill:
name age gender 0 John 30.0 M 1 Jane 30.0 F 2 Mike 30.0 F 3 Susan 40.0 F |
In this example, the missing values in the age and gender columns have been replaced with the previous non-missing values.
Similarly, you can use backward fill (bfill) to replace missing values with the next non-missing values:
# replace missing values in the dataframe using backward filldf_bfill = df.fillna(method='bfill')print(df_bfill) |
This will output a new dataframe with the missing values replaced using backward fill:
name age gender0 John 30.0 M1 Jane 40.0 F2 Mike 40.0 F3 Susan 40.0 F |
In this example, the missing values in the age and gender columns have been replaced with the next non-missing values.