MattR
MattR

Reputation: 5136

Pandas Random ID Category

I would like to be able to assign a PRNG to a dataframe.

I can assign a unique ID using cat.codes or ngroup()

import pandas as pd
import random
import string

df1 = pd.DataFrame({'Name': ['John', 'Susie', 'Jack', 'Jill', 'John']})
df1['id'] = df1.groupby('Name').ngroup()
df1['idz'] = df1['Name'].astype('category').cat.codes

    Name    id  idz
0   John    2   2
1   Susie   3   3
2   Jack    0   0
3   Jill    1   1
4   John    2   2

and I've used a function from this post to create this unique ID row-by-row.

def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
    return ''.join(random.SystemRandom().choice(chars) for _ in range(size))

df1['random id'] = df1['idz'].apply(lambda x : id_generator(3))

    Name    id  idz random id
0   John    2   2   118 #<--- Check Here
1   Susie   3   3   KGZ
2   Jack    0   0   KMQ
3   Jill    1   1   T2L
4   John    2   2   Q3F #<--- Check Here

But how do I combine the two together so that John in this small use-case would recieve the same ID? I'd like to avoid a long if ID not used, then ID, and if name has ID, use existing ID loop if possible due to size of data.

Upvotes: 4

Views: 2359

Answers (2)

J-Eubanks
J-Eubanks

Reputation: 381

Prefacing this with "it's probably not the most efficient option".

I would generate the Random ID's for each unique user by first finding each unique user.

# Finding unique users and storing in a new DataFrame
df_unique_users = pd.DataFrame({'Name':[x for x in set(df['Name'])]})

# Generating unique user ID's for length of data frame 
# By using a set you are guaranteed unique values. You just need to make sure
# you have enough permutations of the unique random_id so that your rand_set 
# will eventually be longer than your unique Names DataFrame. 

rand_set = set()
while(len(rand_set)<len(df_unique_users)):
    rand_set = rand_set.union([id_generator(3)])

df_unique_users['Rand_ID'] = rand_set

### Mapping the random ID's over to the original DataFrame
df = df.merge(df_unique_users, how='left', left_on='Name', right_on='Name')

You could similarly use your original ID columns rather than the Name column to get your unique values.

Upvotes: 0

BENY
BENY

Reputation: 323326

gourpby + transform

df1['random id'] = df1.groupby('idz').idz.transform(lambda x : id_generator(3))
df1
Out[657]: 
    Name  id  idz random id
0   John   2    2       35P
1  Susie   3    3       6UU
2   Jack   0    0       XGF
3   Jill   1    1       5LC
4   John   2    2       35P

Upvotes: 4

Related Questions