Skip to content
Advertisement

Creating a function to standardize categorical variables (python)

I don’t know if it is right to say “standardize” categorical variable string, but basically I want to create a function to set all observations F or f in the column below to 0 and M or m to 1:

JavaScript

I tried this:

JavaScript

But I got an error:

JavaScript

Any ideas? Thanks!

Advertisement

Answer

There is no replace function defined in your code.

Back to your goal, use a vector function.

Convert to lower and map f->0, m->1:

JavaScript

Or use a comparison (not equal to f) and conversion from boolean to integer:

JavaScript

output:

JavaScript

generalization

you can generalize to ant number of categories using pandas.factorize. Advantage: you will get a real Categorical type.

NB. the number values is set depending on whatever values comes first, or lexicographic order if sort=True:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement