Hash each value in a pandas data frame

Question:

In python, I am trying to find the quickest to hash each value in a pandas data frame.

I know any string can be hashed using:

hash('a string')

But how do I apply this function on each element of a pandas data frame?

This may be a very simple thing to do, but I have just started using python.

Asked By: user3664020

||

Answers:

Pass the hash function to apply on the str column:

In [37]:

df = pd.DataFrame({'a':['asds','asdds','asdsadsdas']})
df
Out[37]:
            a
0        asds
1       asdds
2  asdsadsdas
In [39]:

df['hash'] = df['a'].apply(hash)
df
Out[39]:
            a                 hash
0        asds  4065519673257264805
1       asdds -2144933431774646974
2  asdsadsdas -3091042543719078458

If you want to do this to every element then call applymap:

In [42]:

df = pd.DataFrame({'a':['asds','asdds','asdsadsdas'],'b':['asewer','werwer','tyutyuty']})
df
Out[42]:
            a         b
0        asds    asewer
1       asdds    werwer
2  asdsadsdas  tyutyuty
In [43]:

df.applymap(hash)
​
Out[43]:
                     a                    b
0  4065519673257264805  7631381377676870653
1 -2144933431774646974 -6124472830212927118
2 -3091042543719078458 -1784823178011532358
Answered By: EdChum

Pandas also has a function to apply a hash function on an array or column:

import pandas as pd

df = pd.DataFrame({'a':['asds','asdds','asdsadsdas']})
df["hash"] = pd.util.hash_array(df["a"].to_numpy())

Answered By: bert wassink

In addition to @EdChum a heads-up: hash() does not return the same values for a string for each run on every machine. Depending on your use-case, you better use

import hashlib

def md5hash(s: str): 
    return hashlib.md5(s.encode('utf-8')).hexdigest() # or SHA, ...

df['a'].apply(md5hash)
# or
df.applymap(md5hash)
Answered By: Michael Dorner
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.