A product list was typed in by hand. Each label holds a category and a product name joined by a colon, with stray spaces, mixed capitals and underscores instead of spaces, for example " Electronics:wireless_MOUSE ". You want two tidy columns and only the products whose name matches a search word.
Write clean_products(df, keyword).
df has the columns sku (text) and label (text). Every label contains exactly one colon, and nothing is missing. keyword is a word made of letters only.
Return a new DataFrame with the columns sku, category, name, in that order, where:
category is the part of label before the colon, with the spaces around it removed, in lower case;name is the part after the colon, with every underscore replaced by a space, the spaces around it removed, and in title case (each word starts with a capital and continues in lower case);name contains keyword are kept. Upper and lower case are treated as the same when matching.The rows stay in their original order.
name only. A keyword that appears in the category but not in the name does not keep the row."art" matches "Cart".df. If you add columns to a table that you made by selecting some rows or columns of df, call .copy() on that selection first. Without it pandas 2 can warn that the change may not reach the original table.