df = pd.DataFrame([
['Africa', 'Egypt', 0, 0,155490,1,0.115],
['Africa', 'Kenya', 78182, 7,291210,1,0.45],
['Asia Pacific (excludes Greater China)', 'India', 667657, 1,0,-0.650],
['Asia Pacific (excludes Greater China)', 'Vietnam', 0, 0,0,0,0.005]
df.columns = ['Subregion', 'Market', 'Q4 2017', 'Rank_2017','Q4 2018','Rank_2018','year on Year Growth']

I just made up the data in actuality there are much more columns and rows but I Want to know how can I encode these Subregion and Market columns using one-hot encoding or any other way or method or library or work around anything that would do

  • $\begingroup$ Maybe this can help you : datascience.stackexchange.com/a/79575/101580 $\endgroup$
    – Adept
    Commented Jul 21, 2022 at 10:27
  • $\begingroup$ What is the objective? Maybe, in some cases, it is not categories but rather labels. $\endgroup$ Commented Jul 22, 2022 at 8:13
  • $\begingroup$ so my objective was to encode these columns using one hot encoding so I tried and did it but it gave me a 1D array that was not acceptable by the regression to train itself had it been a single column these would have been really easy but here we have 2 column that has categorical data and m not able to perform the learning on these datasets $\endgroup$ Commented Jul 22, 2022 at 17:32
  • 1
    $\begingroup$ It's normal that the one-hot encoding (OHE) gives you an array of features, this is exactly how OHE works. One must concatenate these features to the other features in order to provide a one-dimension array of features for every instance. Also don't forget to use @somebody when you want to reply to a comment, otherwise the other person is not notified. $\endgroup$
    – Erwan
    Commented Jul 25, 2022 at 8:28

1 Answer 1


it totally depends upon how many unique values does your categorical column has

if your categorical column has :

-> more number of unique values and no ranking then you should not prefer one hot encoding has it increases the number of columns

-> only few( considering 2 or 3 unique values) consider nominal coding

-> if it has any ordering or ranking then refer ordinal encoding technique

-> target ordinal encoding technique totally depends upon the usecase , so generally avoid it


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.