A dictionary is a mutable mapping from unique keys to values. Keys can identify translations, columns, file formats, or operations; values can be strings, pandas methods, or Series. section 5.13 section 5.14
Standardizing inconsistent values
A color dictionary maps observed inputs to preferred outputs: {'WH': 'WHITE', 'GY': 'GRAY', 'BK': 'BLACK'}. Applying df['Vehicle Color'].replace(colormap) returns a transformed Series that can be assigned back to the column. Values absent from the dictionary remain unchanged, while matching is precise about whitespace, punctuation, and case. section 5.14
Dictionaries support several practical data tasks:
- Translate messy categorical values into consistent labels, then extend the mapping as new errors appear. section 5.14
- Construct a DataFrame by mapping column names to equal-length Series, such as trip_distance and passenger_count. section 2.18
- Store file-format names as keys and writing methods as values, then iterate through the mapping instead of repeating benchmarking code. section 12.6
Dispatching file writers
write_methods = {'JSON': df.to_json, 'CSV': df.to_csv, 'feather': df.to_feather}. A loop retrieves each format and method, invokes the method with a generated filename, and measures elapsed time. The same pattern works for read_methods, whose values are pandas reader functions. section 12.6
colormap = {'WH': 'WHITE', 'GY':'GRAY', 'BK':'BLACK',
'BL':'BLUE', 'RD':'RED', 'SILVE':'SILVER',
'GR':'GRAY', 'TN':'TAN', 'BR':'BROWN',
'YW':'YELLO', 'BLK':'BLACK', 'GRY':'GRAY',
'WHT':'WHITE', 'WHI':'WHITE', 'OR':'ORANGE',
'BK.':'BLACK', 'WT':'WHITE', 'WT.':'WHITE'}
Inspect a concrete color-normalization dictionary before applying it to a pandas column. section 5.14









































