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

18 Views Asked by At

A number of values in column have similar but differently spelled names.

How do I combine the different spellings as one categorical value?

array(['Individual', 'Trust', 'LLC', nan, 'individual', 'Partnership', 'INdividual', 'Corporation', 'Individual ', 'Corporation ', 'Trust '], dtype=object)

I want to combine all spellings of individual, corporation, and trust. Then I want to combine all individual and non-trusts as one new dummy variable.

I found 'Rename Misspelled Categorical values in Python'this, but the code didn't seem applicable. Also, looked up lambda functions.

1

There are 1 best solutions below

0
Shabazz On

#replace various substrings

#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')