我试图在导入的csv文件中修改和添加一些列。我想要两个额外的列,一个列具有电子邮件地址的MD5值,另一个列具有电子邮件的SHA256值。
+----+-----------+---------+
| id | email | status |
| 1 | 1@foo.com | ERROR |
| 2 | 2@foo.com | SUCCESS |
| 3 | 3@bar.com | SUCCESS |
+----+-----------+---------+我试过了
df['email_md5'] = md5_crypt.hash(df[df.email])这使我错误地说:
KeyError:“1@foo.com”2@foo.com\n '3@bar.com‘不在索引中“
我在另一篇文章中看到了Pandas KeyError: value not in index建议使用reindex,但我无法让它起作用。
发布于 2018-11-04 12:41:56
如果您正在寻找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/https://stackoverflow.com/questions/53140837
复制相似问题