0

I have column like username

UserName , exam, score
Peter, 1, 70
Richard, 2, 80
Peter, 2, 60

...

would like to convert sth like below and pass as input array for model UserName , exam,score

1,1,70
2,2,80
1,2,60

i can create a new col and do mapping by {'Peter':'1','Richard':'2'....} but any smarter code can do this? or Keras have some API to do that I have around 100 user name in the column

Actually I also think so use function concept like below

def NameToInt(pd):
    listName=[]
    i=0
    for x in pd.unique():
        listName.append('X':'i')
        i=i+1
    return listName

Thanks
Rakesh
  • 81,458
  • 17
  • 76
  • 113
Kenneth Yeung
  • 35
  • 1
  • 7
  • I'd say the duplicate provided by @jezrael is incorrect. How is it a duplicate? – Lukasz Tracewski May 24 '18 at 11:38
  • @LukaszTracewski - Why do you think so? – jezrael May 24 '18 at 11:39
  • @jezrael The author needs to encode categorical variables as integers. Moreover, it is desired that the transformation has an easy inverse. How the provided duplicate answers helps in that? LabelEncoder from scikit-learn is what is needed here. – Lukasz Tracewski May 24 '18 at 11:42
  • @LukaszTracewski - be free add link to dupes, obviusly more solutions is possible. Or give me link, i can add it to dupes. – jezrael May 24 '18 at 11:43
  • @jezrael I could not find something that would directly answer the question, but this one is fairly good: https://stackoverflow.com/questions/24458645/label-encoding-across-multiple-columns-in-scikit-learn Here's an exact solution: https://gist.github.com/tracek/463f85eed935440ba0193f33547cef45 – Lukasz Tracewski May 24 '18 at 11:47
  • @LukaszTracewskThanks, LabelEncoder is what I want – Kenneth Yeung May 24 '18 at 14:50

0 Answers0