且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 apply 散列 pandas 数据框列的每一行

更新时间:2023-12-04 10:09:28

您可以创建命名函数并应用它 - 或应用 lambda 函数.在任何一种情况下,都要对数据帧进行尽可能多的处理.

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.

基于 lambda 的解决方案:

A lambda-based solution:

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

命名函数解决方案:

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