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
6
  • I'd say the duplicate provided by @jezrael is incorrect. How is it a duplicate? Commented May 24, 2018 at 11:38
  • @LukaszTracewski - Why do you think so? Commented May 24, 2018 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. Commented May 24, 2018 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. Commented May 24, 2018 at 11:43
  • @jezrael I could not find something that would directly answer the question, but this one is fairly good: stackoverflow.com/questions/24458645/… Here's an exact solution: gist.github.com/tracek/463f85eed935440ba0193f33547cef45 Commented May 24, 2018 at 11:47

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.