How to reshape the data become one hot encoder in python?

Viewed 192

I was a noob in python.Here I have a question about how to reshape the data into one hot encoder in python. The data where the number in userid are repeatable like this:

userid  firstlogin
15605720    2017/4/20 0:00
24537631    2017/4/20 0:00
34012219    2017/4/20 0:00
……
32972232    2017/5/20 23:59
25698417    2017/5/20 23:59
31837935    2017/5/20 23:59
34092811    2017/5/20 23:59

I want to convert it into one hot encoder like this:

userid  2017/4/20   2017/4/21   ……
25181024    1   0
21631446    1   1
31784326    0   1
32258942    1   1
12840021    1   1
32709432    1   1
……

Above all, which function or method should I learn to deal with this problem. Thanks so much!

1 Answers

First step is to extract the date part from firstlogin. Then convert it to object and then create dummy variables using pivot.

oh['dt']=oh['firstlogin'].dt.date
oh['dummies'] = 1
oh.pivot(index='userid', columns='dt', values='dummies')

In general, you should share the progress you have made and describe the bottleneck. Without this, experts would not respond and that could be reason for no response so far.

Related