MultiIndex look up all indices that have a given value for a given level
<p>I am using a <code>pandas.Series</code> with a <code>MultiIndex</code> for a bidirectional weighted lookup. I thought it should be easy to also find the corresponding other levels for a given level using the MultiIndex, but I cannot find a simple function <code>other</code> that does something like the following:</p>

<pre><code>&gt;&gt;&gt; index=pandas.MultiIndex.from_tuples(
...                  [(0, 0),(1,2),(3,4),(5,6),(5,7),(8,0),(9,0)],
...                  names=["concept", "word"])
&gt;&gt;&gt; other(index, "word", 0)
{0, 8, 9}
&gt;&gt;&gt; other(index, "concept", 3)
{4}
&gt;&gt;&gt; other(index, "word", 6)
{5}
</code></pre>

<p>I'm happy to specify level numbers instead of level names and to get any iterable out, not necessarily a set. I only have a 2-level multi-index, so I don't care about how to generalize to a higher-level multi-indices, or even whether it does generalize.</p>

<p>I would be slightly unhappy if this involves iterating over all entries in the MultiIndex and comparing them, because I thought indices are somewhat like multi-key hash tables.</p>

<p>How about this:</p>

<pre><code>&gt;&gt;&gt; index.get_level_values('concept').values[index.get_level_values('word').values == 0]
array([0, 8, 9])

&gt;&gt;&gt; index.get_level_values('concept').values[index.get_level_values('word').values == 6]
array([5])

&gt;&gt;&gt; index.get_level_values('word').values[index.get_level_values('concept').values == 3]
array([4])
</code></pre>

<hr>

<p>Note that you can easily transform a numpy array to a set:</p>

<pre><code>&gt;&gt;&gt; set(np.array([1, 2, 3]))
{1, 2, 3}
</code></pre>

<p>and wrapping all of the above into some function <code>other</code> shouldn't be very difficult.</p>

python pandas indexing multi-index