3
$\begingroup$

So I am new to all this. I was wondering in pandas can I convert my column values into numbers? I'll try and give an example to explain what I mean

So say for example I have a column called, 'animals', in this column I have six different animals but I want to convert them to numerical values so just as simple as 1,2,3,4,5,6 for each of the different animals. How would I go about doing this??

$\endgroup$

3 Answers 3

2
$\begingroup$

Well, one way i like to handle this problem (which is a common problem, at least in daily job life) is to convert each possibility in a column with binary value. Let me elaborate a bit. Let's say you have your column animals with 3 possibilities : dog, cat, and horse. You explode your column in 3 differents columns : colDog, colCat and colHorse. And you fill your new columns based on the value of the column animals. For example : if you have dog in the first row, you put 1 in the column colDog, etc.

The problem with handling categorical data with numerical value instead of binary is that you create a hierarchical order between your values. If dog is 1, cat is 2 and horse is 3, then horse will have more impact than cat and dog. Or i think you just want to represent your categories.

$\endgroup$
2
  • $\begingroup$ The technique which you describe is called One Hot Encoding and should be considered for categorical features with not too many labels. $\endgroup$ Commented Nov 8, 2020 at 19:26
  • $\begingroup$ Totally agree. That's what i mainly use but i know that you can have dimensionality problem. But that is, in my opinion, what preserve the most the categorical aspect of the data. $\endgroup$ Commented Nov 8, 2020 at 20:23
0
$\begingroup$

Sure its called label encoding, example from that page:

le = preprocessing.LabelEncoder()

le.fit(["paris", "paris", "tokyo", "amsterdam"])
LabelEncoder()

list(le.classes_)
['amsterdam', 'paris', 'tokyo']

le.transform(["tokyo", "tokyo", "paris"])
array([2, 2, 1]...)

list(le.inverse_transform([2, 2, 1]))
['tokyo', 'tokyo', 'paris']
$\endgroup$
1
  • $\begingroup$ This is for target encoding only as specified per documentation, if for original data only for transforming columns will go with OrdinalEncoder even if the data is not ordinal: scikit-learn.org/stable/modules/generated/… $\endgroup$
    – n1tk
    Commented Nov 8, 2020 at 20:35
0
$\begingroup$

Ensure that you do not add, subtract or perform any sort of mathematical operation on such encoded labels. Because, statistically speaking these are called nominal values and the variable holding such values is called a nominal variable. Why nominal? Because such a variable does not have any particular order for its constituents or values. In contrast are the ordinal variables where the categorical values have an order.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.