-
Book Overview & Buying
-
Table Of Contents
Python in Excel for Data Analytics
By :
Detecting missing values is only half the job. Left in place, they throw off calculations and skew the patterns you are trying to see. Once you find them, you need to decide what to do next.
There are two basic options: remove the rows that contain missing values or fill those values in with a reasonable substitute. The right choice depends on how much data is missing and how important that column is to your analysis.
The simplest approach is to remove any rows where a value is missing. In pandas, dropna() is the method that handles this. It scans your DataFrame for missing values and removes the rows that contain them. By default, it removes any row with at least one missing value across any column, but you can limit it to specific columns using the subset argument. The following code removes only the rows where horsepower is missing:
mpg_clean = mpg_df.dropna(subset=["horsepower"])
before_count = len(mpg_df)
after_count...