0
$\begingroup$

Using Python and utilizing XGBoost for classification. I have three columns that is supposed to be categorical and are ordinal (ranging from 1-5). Since the columns do not contain strings, I was thinking that simply converting them to type category (utilizing Python Pandas) would be enough. Is that the correct logic? Here is my code:

ser = pd.Series([1, 2, 3], dtype='category')

ser = ser.to_frame()
ser = ser.T
type(ser[0][0]) #Returns numpy.int64

Any help would be great!

$\endgroup$

1 Answer 1

1
$\begingroup$

Yes.

This is especially relevant when reading data from sources like CSV files where types are inferred and numerical categories end up being recognized as numerical.

All you need to do in such case:

series.astype('category')

Official Pandas documentation actually uses exactly the case you mentioned. Details: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.astype.html

$\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.