在python pandas的数据框中为具有选定列的每行数据创建哈希值 [英] Create hash value for each row of data with selected columns in dataframe in python pandas

查看:25
本文介绍了在python pandas的数据框中为具有选定列的每行数据创建哈希值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have asked similar question in R about creating hash value for each row of data. I know that I can use something like hashlib.md5(b'Hello World').hexdigest() to hash a string, but how about a row in a dataframe?

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

I have drafted my code as below:

for index, row in course_staff_df.iterrows():
        temp_df.loc[index,'hash'] = hashlib.md5(str(row[['cola','colb']].values)).hexdigest()

对我来说似乎不是很pythonic,有什么更好的解决方案吗?

It seems not very pythonic to me, any better solution?

推荐答案

现在可以在 pandas.util.hash_pandas_object:

This is now available in pandas.util.hash_pandas_object:

pandas.util.hash_pandas_object(df)

这篇关于在python pandas的数据框中为具有选定列的每行数据创建哈希值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆