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的相关部分。

更多相关文章

  1. 【实例】python re 正则表达式 同时选择带有“是”和“的”句子
  2. Python数据挖掘实例(实时更新)
  3. python编程之一:使用网格索引算法进行空间数据查询
  4. 堆和索引堆的python实现
  5. 第四章 当索引不好用时
  6. 使用Python编写简单的端口扫描器的实例分享【转】
  7. 如何正确地获取在pysnmp中被捕获的变量的表行索引和命名值?
  8. UNIX-LINUX编程实践教程->第八章->实例代码注解->写一个简单的sh
  9. Linux编程之《只运行一个实例》

随机推荐

  1. 分享一个生成文件层级树类
  2. PHP中如何使用PDO修改数据?
  3. Linux下查看PHP配置文件php.ini的位置
  4. 基于 Hyperf + RabbitMQ + WebSocket 实
  5. PHP之Category类库 无限分类
  6. PHP == 和 === 区别
  7. PHP之curl_multi并发详解方法
  8. 入阶PHP-FPM
  9. PHP还能开发什么?
  10. php简单图形面积计算器的实现