python - 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.
解决方案
如果您正在寻找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/
推荐阅读
- r - R - 链接矩阵行 - 使代码更快
- java - Linux 上的 JVisualVm
- dart - 迭代颤动图中的元素
- python - PyGeodesy 和 GeographicLib 在 Python 2.7 中可用,但在 Python 3.6 中不可用
- ios - Fabric Crashlytics 中缺少版本
- angular - Typescript,TypeError 不是函数,在同一个类中调用另一个方法
- reactjs - AsyncTypeahead 选项在搜索后立即消失
- android - 无法使用“node fcm-notification”显示 FCM 通知
- sqlite - 如何使用 ClearLinux 编译标志构建 SQLite?
- windows - 以编程方式使 Windows 旧版驱动程序不可停止