transform a dataframe column with a hash value

Like @Ch3steR commented:

This correct assuming every value has a unique “hash value” but there doesn’t exist such hash function as of now. Every hash function is collision prone.

# Example for collision
hash(0.1) == hash(230584300921369408)

Note: From Python 3.3 values of strings and bytes objects are salted with a random value before the hashing process. This means that the value of the string is modified with a random value that changes every time your interpreter starts. This is done to avoid dictionary hash attack

# Example taken martijn's answer:
>>> hash("235")

Now, open a new session.

>>> hash("235")

But if only few rows of data you can use:

Use helper dictionary for hash and then for mapping back swap key:values to d1 dictionary and pass to

d2 = {hash(x):x  for x in df['Student']}
d1 = {v:k for k, v in d2.items()}

df['Studendid']= df['Student'].map(d1)
df['orig']= df['Studendid'].map(d2)
print (df)
  Student            Studendid   orig
0   Stud1  6001180169368329239  Stud1
1   Stud2 -1507322317280771023  Stud2
2   Stud3 -2262724814055039076  Stud3
3   Stud4   364063172999472918  Stud4
4   Stud5  8548751638627509914  Stud5
5   Stud6  5647607776109616031  Stud6
6   Stud7   729989721669472240  Stud7
7   Stud8  4828368150311261883  Stud8
8   Stud9  8466663427818502594  Stud9

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top