Cleaning data is presented as one of the central realities of data analysis: real-world datasets are often incomplete, inconsistent, or inaccurate, even when they come from official-looking sources. The chapter emphasizes that missing values can arise from human error, system failures, or simply the fact that nothing was recorded, and that handling those gaps well is essential if analysis is going to be trustworthy. It frames cleaning as a broad set of tasks, from renaming and restructuring columns to removing bad rows, standardizing text, correcting types, and dealing with outliers.
The chapter then focuses on core pandas tools for inspecting and cleaning data efficiently. It shows how to measure dataset size with len(df.index), identify missing values with isnull() and notnull(), count nulls with .sum(), and use info(show_counts=True) for a quick overview. It also compares ways to remove or replace bad data, such as dropna(), fillna(), replace(), mode(), and pd.to_numeric(errors='coerce'), while stressing that different kinds of data require different decisions. The text repeatedly notes that the “right” fix depends on context, because preserving data, eliminating noise, and avoiding misleading results can all matter at once.
Through exercises, the chapter illustrates these ideas in practical settings. In the parking-ticket example, it measures how much revenue might be lost if rows with missing fields are dropped under different rules, showing how a small percentage of missing data can represent a large financial impact. In the celebrity-deaths example, it extracts month information from dates, cleans an age column that mixes text and numbers, removes implausible ages, and then computes an average over a selected date range. In the Titanic exercise, it demonstrates different choices for handling nulls: dropping some sparse columns, filling ages with the mean, and filling destination data with the mode. A final parking-ticket exercise shows how inconsistent labels, especially vehicle colors, can be normalized with a translation dictionary and replace(), reinforcing the broader lesson that data cleaning is often messy, iterative, and indispensable.
Sample of df, including NaN values
Running dropna on a data frame removes all NaN values and the rows containing them.
Running dropna on a data frame, looking at only a subset of columns
Adding a new month column to our data frame based on the month in dateofdeath
Finding the number of NaN values in a column by summing the result of isnull()
Removing rows in which a column contains NaN
Replacing NaN in the age column with the mean of age
Summary
Cleaning data is one of the most important parts of data analysis, although it’s not glamorous. In this chapter, we saw that effective data cleaning requires not just knowing the techniques, but also applying judgment—knowing when you can allow null or duplicate values and what you should do with them. pandas comes with a wide variety of tools we can use to clean our data, from removing NaN values to replacing them, replacing existing values, and running custom functions on each row in a series or data frame. The techniques we explored in this chapter, along with the interpolate method we saw in exercise 13, are important tools in your data-cleaning toolbox and will likely come up in many of the projects you work on.
Pandas Workout ebook for free