如何将分类变量的多个拼写重命名为一个值

How to rename multiple spellings of a categorical variable as one value

提问人:Shabazz 提问时间:7/19/2023 更新时间:7/19/2023 访问量:9

问:

列中的许多值具有相似但拼写不同的名称。

如何将不同的拼写组合为一个分类值?

array(['个人', '信托', 'LLC', nan, '个人', '合伙企业', '个人', '公司', '个人', '公司', '信任'], dtype=对象)

我想结合个人、公司和信任的所有拼写。 然后,我想将所有个人和非信任合并为一个新的虚拟变量。

我发现了“在 Python 中重命名拼写错误的分类值”但代码似乎不适用。 另外,查找了 lambda 函数。

变量 重命名 分类

评论


答:

0赞 Shabazz 7/19/2023 #1

#replace 各种子字符串

#replace different spellings of individual
df['type'] = df['type'].replace('individual', 'Individual')
df['type'] = df['type'].replace('INdividual', 'Individual')
df['type'] = df['type'].replace('Individual ', 'Individual')

#replace different spellings of trust
df['type'] = df['type'].replace('Trust ', 'Trust')

#replace spellings of corporation
df['type'] = df['type'].replace('Corporation ', 'Corporation')