Hash each value in a pandas data frame

21,885

Solution 1

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

Solution 2

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

Share:
21,885
user3664020
Author by

user3664020

Updated on July 09, 2022

Comments

  • user3664020
    user3664020 almost 2 years

    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.