Skip to content Skip to sidebar Skip to footer

Convert Dataframe Columns Value Into Digital Number

I have following data in my column of data frame. How can I convert each domain name by digital number? I try to use replace in a for loop. However, since I have more than 1200 unq

Solution 1:

Let's use pd.factorize:

df.assign(domain=pd.factorize(df.domain)[0]+1)

Output:

   type  domain
0     1       1
1     1       2
2     0       2
3     0       3
4     0       2
5     0       3
6     1       4
7     1       5
8     1       4
9     1       4

Solution 2:

If it does really matter for the digital number assignment, you can try this

import pandas as pd 

df.domain.astype('category').cat.codes

Out[154]: 
0    4
1    2
2    2
3    0
4    2
5    0
6    1
7    3
8    1
9    1
dtype: int8

If that is matter, you can try

maplist=df[['domain']].drop_duplicates(keep='first').reset_index(drop=True).reset_index().set_index('domain')
maplist['index']=maplist['index']+1
df.domain=df.domain.map(maplist['index'])
   Out[177]: 
   type  domain
0     1       1
1     1       2
2     0       2
3     0       3
4     0       2
5     0       3
6     1       4
7     1       5
8     1       4
9     1       4

Post a Comment for "Convert Dataframe Columns Value Into Digital Number"