I'd like to get the row and column labels for values matching some condition in a dataframe. Just to keep it interesting, I need it to work with a hierarchical (multi-)index. For example:
df = pd.DataFrame(np.arange(16).reshape(4, 4), columns=pd.MultiIndex.from_product((('a', 'b'), ('x', 'y'))))
a b
x y x y
0 0 1 2 3
1 4 5 6 7
2 8 9 10 11
3 12 13 14 15
Now let's say I want the row and column labels of the elements where
df % 6 == 0
a b
x y x y
0 True False False False
1 False False True False
2 False False False False
3 True False False False
I would like to get
[(0, ('a', 'x')), (1, ('b', 'x')), (3, ('a', 'x'))]
Please note I would like a general solution, that does not rely on the index being monotonic, or the particular selection in my example. This questions has been asked many times, but the answers do not generalize:
- index and column for the max value in pandas dataframe: relies on sorting to find max
- Pandas dataframe: return row AND column of maximum value(s): does not generalize
- Retrieve indices of NaN values in a pandas dataframe: does not return row label
- Return list of indices/index where a min/max value occurs in a pandas dataframe: does not generalize
- Pandas: Get each value's index and columns values: does not generalize / uses iteration
Is this really so hard in Pandas?