python - 从列表中查找重复的 csv 列 [python pandas]
问题描述
我想从列表中找到重复的列,而不是任何列。
正确的 csv 示例如下所示:
col1, col2, col3, col4, custom, custom
1,2,3,4,test,test
4,3,2,1,test,test
列表如下所示:
columnNames = ['col1', 'col2', 'col3', 'col4']
因此,当我运行类似df.columns.duplicated()
我不想检测重复的“自定义”字段时,只有当有多个“col1”列或多个“col2”列等时,才返回 True这些列被发现是重复的。
我发现在示例中包含重复的“colN”列名 col4 时,我将其打印出来,它告诉我index(['col1', 'col2', 'col3', 'col4', 'col4.1'], dtype='object')
不知道如何编写那行代码。
解决方案
使用Index.isin
+Index.duplicated
创建一个布尔掩码:
c = df.columns.str.rsplit('.', n=1).str[0]
mask = c.isin(columnNames) & c.duplicated()
如果要查找重复的列名,请boolean indexing
使用mask
:
dupe_cols = df.columns[mask]
推荐阅读
- git - 应用 git patch 会自动添加补丁文件进行暂存?
- odata - 如何覆盖 OData 过滤器查询验证器以允许未定义模型的属性
- microsoft-cognitive - 使用 Face API 来增强身份服务
- symfony - 找不到类“Symfony\Component\HttpFoundation\RedirectResponse”
- apache-spark - 如何在 oozie 工作流程中运行 spark 程序?
- angular - PrimeNG Treetable 子节点发布扩展事件
- apache-spark-sql - SPARK SQL 中的 DATEDIFF
- reactjs - Material UI - 如何在 TableFooter 中将 IconButton 居中?
- java - 同一服务器上的两个项目具有不同的端口但客户端(Angular 6)无法调用服务器(Spring Boot)
- java - Surefire 插件 > 2.19 版和 junit 套件