Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To fill null or missing values in a Pandas DataFrame column with the mode of that column, follow these steps:

  1. Import the Pandas library:

    import pandas as pd
    
  2. Create a DataFrame object or read a CSV file into a DataFrame:

    df = pd.read_csv('data.csv')
    
  3. 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.

  4. 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.