如何在DataFrame中找到重复的索引?
I have a pandas DataFrame with a multi-level index ("instance" and "index"). I want to find all the first-level ("instance") index values which are non-unique and to print out those values.
我有一个带有多级索引(“实例”和“索引”)的pandas DataFrame。我想找到所有非唯一的第一级(“实例”)索引值并打印出这些值。
My frame looks like this:
我的框架看起来像这样:
A
instance index
a 1 10
2 12
3 4
b 1 12
2 5
3 2
b 1 12
2 5
3 2
I want to find "b" as the duplicate 0-level index and print its value ("b") out.
我想找到“b”作为重复的0级索引并打印出它的值(“b”)。
3 个解决方案
#1
2
You can use the get_duplicates()
method:
您可以使用get_duplicates()方法:
>>> df.index.get_level_values('instance').get_duplicates()
[0, 1]
(In my example data 0
and 1
both appear multiple times.)
(在我的示例中,数据0和1都出现多次。)
The get_level_values()
method can accept a label (such as 'instance') or an integer and retrieves the relevant part of the MultiIndex.
get_level_values()方法可以接受标签(例如'instance')或整数,并检索MultiIndex的相关部分。
更多相关文章
- 【实例】python re 正则表达式 同时选择带有“是”和“的”句子
- Python数据挖掘实例(实时更新)
- python编程之一:使用网格索引算法进行空间数据查询
- 堆和索引堆的python实现
- 第四章 当索引不好用时
- 使用Python编写简单的端口扫描器的实例分享【转】
- 如何正确地获取在pysnmp中被捕获的变量的表行索引和命名值?
- UNIX-LINUX编程实践教程->第八章->实例代码注解->写一个简单的sh
- Linux编程之《只运行一个实例》