python - 如何在python pandas中显示重复的ID和重复的数据
问题描述
我有一个如下所示的数据框
k={'ID':[1,2,3,4,5,6],'Name':['John Danny','Micheal K','John Danny','jerred','John Danny','joe'],'phone':['1111',
'2222','2233','1111','2222','6666']}
df=pd.DataFrame(data=k)
df
ID Name phone
1 John Danny 1111
2 Micheal K 2222
3 John Danny 2233
4 jerred 1111
5 John Danny 2222
我需要在数据框中找到重复的姓名和电话,所以使用下面给出的代码
df[df['Name'].duplicated(keep=False)].sort_values("Name")
根据名称重复
ID Name phone
1 John Danny 1111
3 John Danny 2233
5 John Danny 2222
根据手机重复
ID Name phone
1 John Danny 1111
4 jerred 1111
2 Micheal K 2222
5 John Danny 2222
但我想要的结果如下
ID Name phone duplicated of name ids duplicated of phone ids Duplicate_name Duplicate_phone
1 John Danny 1111 3,5 4 Yes Yes
2 Micheal K 2222 5 No Yes
3 John Danny 2233 1,5 Yes No
4 jerred 1111 1 No Yes
5 John Danny 2222 1,3 2 Yes Yes
我能够使用下面的代码找到duplicate_name 和duplicate_phone
df['Duplicate_name'] = df['Name'].duplicated(keep=False).map({True:'Yes', False:'No'})
df['Duplicate_phone'] = df['phone'].duplicated(keep=False).map({True:'Yes', False:'No'})
问题是无法在重复的电话 ID 和重复的姓名 ID 中显示 ID,如上给出的结果表怎么办?
解决方案
GroupBy.transform
与减法的自定义函数一起使用set
:
def f(x):
return [', '.join(set(x) - set([y])) for y in x]
或使用if
生成器过滤:
def f(x):
return [', '.join(z for z in x if z != y) for y in x]
df['duplicated of name ids'] = df['ID'].astype(str).groupby(df['Name']).transform(f)
df['duplicated of phone ids'] = df['ID'].astype(str).groupby(df['phone']).transform(f)
df['Duplicate_name'] = df['Name'].duplicated(keep=False).map({True:'Yes', False:'No'})
df['Duplicate_phone'] = df['phone'].duplicated(keep=False).map({True:'Yes', False:'No'})
print (df)
ID Name phone duplicated of name ids duplicated of phone ids \
0 1 John Danny 1111 5, 3 4
1 2 Micheal K 2222 5
2 3 John Danny 2233 5, 1
3 4 jerred 1111 1
4 5 John Danny 2222 1, 3 2
5 6 joe 6666
Duplicate_name Duplicate_phone
0 Yes Yes
1 No Yes
2 Yes No
3 No Yes
4 Yes Yes
5 No No
推荐阅读
- twitter-bootstrap - 使用相同的模态制作不同的链接,每个链接显示不同的轮播
- javascript - 将数组拆分为总和等于特定目标的数字数组
- python - 在 Windows 上通过 pip 为 Python 3.9 安装 pyodbc 时出错
- python - 使用 python 批量更新优化 Elasticsearch 更新脚本主体
- xaml - 使用标准按钮创建单选按钮组
- r - 对数据集进行分层采样并对训练数据集中的变量进行平均
- node.js - 在公共 HTML 文件夹中安装 NodeJS 是否存在安全隐患?
- reactjs - Android 10 上的 Chrome 85 打破了 PWA?
- python - 工具链 Python(带有 Kivy)代码 -> Xcode iOS 模拟器 -> “ModuleNotFoundError: No module named 'kivy'”
- python-2.7 - 如何在 MAYA 中的 python 脚本之间传递参数?