Create hash value for each row of data with selected columns in dataframe in python pandas

前端 未结 6 829
北恋
北恋 2020-12-06 10:18

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()

6条回答
  •  独厮守ぢ
    2020-12-06 11:09

    I've came up with this adaption from the code provided on the question:

    new_df2 = df.copy()
    key_combination = ['col1', 'col2', 'col3', 'col4']
    new_df2.index = list(map(lambda x: hashlib.sha1('-'.join([col_value for col_value in x]).encode('utf-8')).hexdigest(), new_df2[key_combination].values))
    

提交回复
热议问题