首页 > 解决方案 > Python Pandas MD5 Value not in index

问题描述

I'm trying to modify and add some columns in an imported csv file. The idea is that I want 2 extra columns, one with the MD5 value of the email address, and one with the SHA256 value of the email.

+----+-----------+---------+
| id | email     | status  |
| 1  | 1@foo.com | ERROR   |
| 2  | 2@foo.com | SUCCESS |
| 3  | 3@bar.com | SUCCESS |
+----+-----------+---------+

I have tryed with

df['email_md5'] = md5_crypt.hash(df[df.email])

This gives me an error saying:

KeyError: "['1@foo.com' '2@foo.com'\n '3@bar.com'] not in index"

I have seen in another post Pandas KeyError: value not in index its suggested to use reindex, but I can't get this to work.

标签: pythonpandascsvmd5

解决方案


如果您正在寻找md5_crypt.hash,您必须将 md5_crypt 模块的散列函数应用到每封电子邮件,使用pd.apply()-

from passlib.hash import md5_crypt
df['email_md5'] = df['email'].apply(md5_crypt.hash)

输出

id  email   status  email_md5
1   1@foo.com   ERROR    11 lHP8aPeE$5T4jqc/qir9yFszVikeSM0
2   2@foo.com   SUCCESS  11 jyOWkcrw$I8iStC3up3cwLLLBwnT5S/
3   3@bar.com   SUCCESS  11 oDfnN5UH$/2N6YljJRMfDxY2gXLYCA/

推荐阅读