使用 apply 散列 pandas 数据框列的每一行 [英] Hash each row of pandas dataframe column using apply
问题描述
我正在尝试使用数据帧列 ORIG 上的以下算法对 python 3.6 pandas 数据帧列的每个值进行散列:
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(hashlib.sha1(str(df.ORIG).encode("UTF-8")).digest())
但是,上面提到的代码没有对列的每个值进行哈希处理,因此,为了对 df-column ORIG 的每个值进行哈希处理,我需要使用 apply 函数.不幸的是,我似乎还不够好,无法完成这项工作.
However, the above mentioned code does not hash each value of the column, so, in order to hash each value of the df-column ORIG, I need to use the apply function. Unfortunatelly, I don't seem to be good enough to get this done.
我想象它看起来像下面的代码:
I imagine it to look like the following code:
df["HK_ORIG"] = str(df['ORIG']).encode("UTF-8")).apply(hashlib.sha1)
我非常期待您的回答!非常感谢!
I'm looking very much forward to your answers! Many thanks in advance!
推荐答案
您可以创建命名函数并应用它 - 或应用 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)
这篇关于使用 apply 散列 pandas 数据框列的每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!