Hash each row of pandas dataframe column using apply

后端 未结 1 950
执念已碎
执念已碎 2020-12-21 03:04

I\'m trying to hash each value of a python 3.6 pandas dataframe column with the following algorithm on the dataframe-column ORIG:

HK_ORIG = base64.b64encode(         


        
相关标签:
1条回答
  • 2020-12-21 03:29

    You can either create a named function and apply it - or apply a lambda function. In either case, do as much processing as possible withing the dataframe.

    A lambda-based solution:

    df['ORIG'].astype(str).str.encode('UTF-8')\
              .apply(lambda x: base64.b64encode(hashlib.sha1(x).digest()))
    

    A named function solution:

    def hashme(x):
        return base64.b64encode(hashlib.sha1(x).digest())
    df['ORIG'].astype(str).str.encode('UTF-8')\
              .apply(hashme)
    
    0 讨论(0)
提交回复
热议问题