liveBook · Manning

Data cleaning

As explained in The Quick Python Book, Fourth Edition

Data cleaning is the process of identifying and correcting, removing, or otherwise handling irregular, invalid, missing, or inconvenient data values and formats.

Data cleaning handles values and formats that would otherwise disrupt processing: missing markers, invalid characters, extra whitespace, awkward ordering, and numbers stored as text. It is often part of transforming a data file into a consistent, usable format. section 21.4 section 21.7

Choose a treatment that fits the next operation:

  • Replace a marker such as "Missing" with None or an empty string, or leave it in place and skip that value during calculations. section 21.4.1
  • Remove a suffix such as % before converting its text to a number; divide by 100 if the result should be a fraction. section 24.3.2
  • Discard fields you do not need, and filter out records or trailing documentation that do not belong in the output. section 21.6.2

Make two fields usable for calculations

A heat-index field may contain "Missing", while a coverage field contains text such as "35.11%": row = ["Missing", "35.11%"] heat = None if row[0] == "Missing" else float(row[0]) coverage = float(row[1].replace("%", "")) / 100 print(heat, coverage) # None 0.3511 Exclude the missing heat value when computing an average; do not pass it to arithmetic as though it were a temperature. section 21.4.1 section 24.3.2

import csv
 
with (open("Illinois_weather_1979-2011.txt") as infile,
      open("Illinois_weather_1979-2011.csv", "w") 
                                 as outfile):                #1
    for row in csv.reader(infile, delimiter="\t"):
        if row[0] == '---':                                  #2
            break
        if "Cook" in row[3]:
            del row[0:2]                                     #3
            row = [item.replace("Missing", "") 
                                 for item in row]            #4
            row[-1] = row[-1].replace("%", "")               #5
            row[-1] = (float(row[-1])/100 if str.isdigit(row[-1]) 
                           else row[-1])                     #6
            csv.writer(outfile).writerow(row)

For a complete file pass, this Python csv example reads tab-delimited rows, stops at a documentation separator, selects the desired county, removes fields, cleans values, and writes CSV rows. section 21.6.2

Pandas offers a column-wise alternative. When loading a CSV, use read_csv with na_values=['Missing'] to turn that marker into NaN, and use usecols to omit unwanted columns. For a percentage column, strip %, convert with pd.to_numeric, then divide by 100. Plain Python remains an option; pandas is not the best fit for every dataset or near-real-time task. section 24.3.2 section 24.7

temp[17] = pd.to_numeric(temp[17].str.strip("%")).div(100)

This compact pandas expression combines stripping the suffix, numeric conversion, and conversion to a fraction for one column. section 24.3.2

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Works through missing-value choices, row-wise file cleanup, and pandas column conversions; does not prescribe one cleaning policy for every dataset.
Examples
Uses weather observations containing "Missing" heat-index values and percentage strings, plus a tab-delimited county-to-CSV conversion.
What it assumes
Assumes familiarity with Python file-processing basics; demonstrates pandas cleaning through concrete CSV columns.
What you can do after
Apply it via Python’s csv module or pandas to handle missing markers, convert percentages, and produce usable CSV data.
How it connects
Connects data cleaning to ETL, CSV processing, missing values, numeric conversion, and pandas DataFrames.

Also explained in

Compare how 14 books explain data cleaning →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.