You want factorize
to encode the values to ints:
In [52]:
df['site_id'] = pd.factorize(df['site_id'])[0] + 1
df
Out[52]:
user_id site_id
0 100 1
1 100 2
2 100 1
3 101 2
4 101 2
5 101 2
here factorize
returns an array:
In [53]:
pd.factorize(df['site_id'])
Out[53]:
(array([0, 1, 0, 1, 1, 1], dtype=int64), Int64Index([1, 2], dtype='int64'))
we want the encoded values in the tuple and add 1
to each:
pd.factorize(df['site_id'])[0] + 1
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…