1 | initial version |
To fill null or missing values in a Pandas DataFrame column with the mode of that column, follow these steps:
Import the Pandas library:
import pandas as pd
Create a DataFrame object or read a CSV file into a DataFrame:
df = pd.read_csv('data.csv')
Use the fillna()
method of the DataFrame object to fill null or missing values with the mode of the column:
df['column_name'].fillna(df['column_name'].mode()[0], inplace=True)
Here, column_name
is the name of the column we want to fill null or missing values, mode()[0]
returns the mode value of the column, and inplace=True
ensures that the changes are made to the original DataFrame.
Finally, we can check the DataFrame for any missing values using the isnull()
method:
df.isnull().sum()
This will return the number of missing values in each column of the DataFrame, which should be 0 for the column we just filled with the mode.