Pandas/Cleaning data 8 min

Text and categorical columns

The .str accessor and ordered categories.

strcategoryextract

The .str accessor applies string methods element-wise, vectorised and NaN-safe.

python · editable
loading editor…

Try it: Keep only the products whose name ends with 'r'.

str.split with expand=True turns one column into several, and str.extract pulls out a regex group.

python · editable
loading editor…

An ordered categorical gives text a meaningful sort order, so 'low, medium, high' no longer sorts alphabetically.

python · editable
loading editor…