Instagram
youtube
Facebook
Twitter

Drop Rows with Missing Values from a Pandas DataFrame

    Drop Rows with Missing Values from a Pandas DataFrame
     
     Short Description of the Program:

     This program removes all rows from the DataFrame that contain missing (NaN) values.
   
      Explanation:

  • dropna()
    Automatically removes any row that has one or more NaN values.

     
  • Use inplace=True if you want to modify the original DataFrame:

    python
    CopyEdit

Program:

import pandas as pd

import numpy as np

data = {

    'Name': ['Alice', 'Bob', 'Charlie', 'Diana', 'Eva', 'Frank'],

    'Age': [25, np.nan, 22, 28, 26, 35],

    'Gender': ['Female', 'Male', 'Male', 'Female', 'Female', 'Male'],

    'City': ['New York', 'Los Angeles', np.nan, 'Houston', 'Phoenix', 'Seattle']

}

df = pd.DataFrame(data)

cleaned_df = df.dropna()

print(cleaned_df)