为python中dataframe中具有选定列的每行数据创建哈希值

我在R中提出了类似的问题,关于为每行数据创建哈希值。我知道我可以使用类似于hashlib.md5(b'Hello World').hexdigest()的东西来散列字符串,但是数据帧中的一行呢

更新01

我已经起草了我的代码如下:

对于课程_staff_df.iterrows()中的索引行:
temp_df.loc[index,'hash']=hashlib.md5(str(row['cola','colb']].values)).hexdigest()

对我来说,这似乎不太像蟒蛇,有更好的解决办法吗

或者简单地说:

df.apply(lambda x:hash(tuple(x)),axis=1)

例如:

将熊猫作为pd导入
将numpy作为np导入
df=pd.DataFrame(np.random.rand(3,5))
打印df
apply(lambda x:hash(tuple(x)),axis=1)
0         1         2         3         4
0  0.728046  0.542013  0.672425  0.374253  0.718211
1  0.875581  0.512513  0.826147  0.748880  0.835621
2  0.451142  0.178005  0.002384  0.060760  0.098650
0    5024405147753823273
1    -798936807792898628
2   -8745618293760919309

发表评论