Missing Values
Missing values are common in data cleaning activities and can occur for various reasons. Understanding the nature and handling of missing data is crucial for accurate data analysis.
Types of Missing Data
- Missing at Random (MAR): Missing values can be predicted by other variables in the dataset.
- Missing Completely at Random (MCAR): Missing values have no relationship to any other variables.
Reasons for Missing Data
- Survey omissions: Respondents might skip questions.
- Data collection errors: Some data might not be collected due to process errors.
- Irrelevant data: Certain data points might not make sense to collect (e.g., students generally don't have office locations in a university database).
Handling Missing Data in Pandas
Detecting Missing Values
Pandas can detect missing values from various data formats like CSV files, which often label missing values as NaN, NULL, None, or N/A. However, some datasets might use unconventional labels (e.g., 99 for missing binary categories). The read_csv function's na_values parameter can specify these forms.
df = pd.read_csv('datasets/class_grades.csv', na_values=[99])
Checking for Missing Values
The .isnull() function creates a boolean mask indicating the presence of missing values.
mask = df.isnull()
print(mask.head(10))
Dropping Missing Values
The dropna() function removes rows with any missing data.
df_clean = df.dropna()
print(df_clean.head(10))