16

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.

EdChum
  • 376,765
  • 198
  • 813
  • 562
user3664020
  • 2,980
  • 6
  • 24
  • 45

3 Answers3

24

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
EdChum
  • 376,765
  • 198
  • 813
  • 562
7

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())

Wildhammer
  • 2,017
  • 1
  • 27
  • 33
bert wassink
  • 350
  • 3
  • 9
7

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)

or set the PYTHONHASHSEED environment variable. For those wondering why this behaviour exists, it is protection against malicious attackers sending keys designed to collide.

Philip Kendall
  • 4,304
  • 1
  • 23
  • 42
Michael Dorner
  • 17,587
  • 13
  • 87
  • 117