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.
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.
You can use the get_duplicates()
method:
>>> df.index.get_level_values('instance').get_duplicates()
[0, 1]
(In my example data 0
and 1
both appear multiple times.)
The get_level_values()
method can accept a label (such as 'instance') or an integer and retrieves the relevant part of the MultiIndex.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With