gpt4 book ai didi

python - 在 Pandas 中每行创建一个唯一值?

转载 作者:行者123 更新时间:2023-11-28 16:26:50 24 4
gpt4 key购买 nike

获取原始数据 --> 对其进行转换并将其与其他文件合并 --> 通过电子邮件发送给最终用户以供审阅

什么是最好的方法?

最佳答案

如果 'employee_id'+'customer_id'+'timestamp' 很长,并且您对一些不太可能有冲突的东西感兴趣,您可以将其替换为哈希。散列的范围和质量将决定冲突的概率。也许最简单的是使用 builtin hash .假设你的 DataFrame 是 df,列是字符串,这是

(df.employee_id + df.customer_id + df.timestamp).apply(hash)

如果您想更好地控制大小和碰撞概率,请参阅 this piece on non-crypotgraphic hash functions in Python .


编辑

基于 an answer to this question ,您可以像这样构建 10 个字符的哈希值:

import hashlib
df['survey_id'] = (df.employee_id + df.customer_id + df.timestamp).apply(
lambda s: hashlib.md5(s).digest().encode('base64')[: 10])

关于python - 在 Pandas 中每行创建一个唯一值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35898690/

24 4 0
文章推荐: html - AngularJS - 动态更新 ng 类名称
文章推荐: javascript - 如何在