如何更轻松地将类别分配给包含 50 多个类别的新列中的字符串

How to more easily assign a category to a string in a new column with over 50 categories

我有一个数据框,其中有一列开放式响应字符串,用于标识美国的一个州(希望这会很快成为一个封闭式问题)。我需要为每个响应分配一个状态名称,目前正在使用以下代码。

alabama_cat = ["alabama", "al"]
alaska_cat = ["alaska", "ak"]
newyork_cat = ["new york", "ny", "newyork"]

state_cat = [alabama_cat, alaska_cat, newyork_cat]

#Conditions for categories
conditions = [
    (survey['state'].str.lower().str.contains('|'.join(alabama_cat), na=False)),
    (survey['state'].str.lower().str.contains('|'.join(alaska_cat), na=False)),
    (survey['state'].str.lower().str.contains('|'.join(newyork_cat), na=False)),
]

#Names of categories
choices = ["Alabama", "Alaska", "New York"]

# categorize
survey['state_category'] = np.select(conditions, choices)

我想知道是否有更简单的方法来创建条件变量,我希望找到一种自动方法 运行 每个 state_cat 到 (survey['state'].str.lower().str.contains('|'.join(alabama_cat), na=False))。我需要 运行 每个州和可能的领土以及人们输入其他国家的情况的这个过程。

非常感谢您的任何见解。

无需检查每个 cat,您可以尝试提取任何一只猫,然后使用 map。像这样:

# map the codes to actual names
state_codes = {code:choice for cat,choice in zip(state_cat, choices) 
                 for code in cat}

patt = '|'.join(state_codes.keys())

survey['state_category'] = survey['state'].str.extract(f'({patt})', expand=False).map(state_codes)