Iterating over multiIndex dataframe
<p>I have a data frame as shown below
<img src="http://i.stack.imgur.com/H6Sh0.png" alt="dataframe"></p>

<p>I have a problem in iterating over the rows. for every row fetched I want to return the key value. For example in the second row for <strong>2016-08-31 00:00:01</strong> entry df1 &amp; df3 has compass value 4.0 so I wanted to return the keys which has the same compass value which is df1 &amp; df3 in this case
I Have been iterating rows using </p>

<pre><code>for index,row in df.iterrows():
</code></pre>

<p><strong>Update</strong></p>

<p>Okay so now I understand your question better this will work for you.
First change the shape of your dataframe with</p>

<pre><code>dfs = df.stack().swaplevel(axis=0)
</code></pre>

<p>This will make your dataframe look like:</p>

<p><a href="http://i.stack.imgur.com/UaOj7.png" rel="nofollow"><img src="http://i.stack.imgur.com/UaOj7.png" alt="enter image description here"></a></p>

<p>Then you can iterate the rows like before and extract the information you want. I'm just using print statements for everything, but you can put this in some more appropriate data structure.</p>

<pre><code>for index, row in dfs.iterrows():
     dup_filter = row.duplicated(keep=False)
     dfss = row_tuple[dup_filter].index.values
     print("Attribute:", index[0])
     print("Index:", index[1])
     print("Matches:", dfss, "\n")
</code></pre>

<p>which will print out something like</p>

<pre><code>.....

Attribute: compass
Index: 5
Matches: ['df1' 'df3']

Attribute: gyro
Index: 5
Matches: ['df1' 'df3']

Attribute: accel
Index: 6
Matches: ['df1' 'df3']

....
</code></pre>

<p>You could also do it one attribute at a time by </p>

<p><code>dfs_compass = df.stack().swaplevel(axis=0).loc['compass']</code></p>

<p>and iterate through the rows with just the index.</p>

<p><strong>Old</strong></p>

<p>If I understand your question correctly, i.e. you want to return the indexes of rows which have matching values on the second level of your columns, i.e. ('compass', 'accel', 'gyro'). The following will work.</p>

<pre><code>compass_match_indexes = []

for index, row in df.iterrows():
    match_filter = row[:, 'compass'].duplicated()
    if len(row[:, 'compass'][match_filter] &gt; 0)
        compass_match_indexes.append(index)
</code></pre>

<p>You can use select your dataframe with that list like <code>df.loc[compass_match_indexes]</code></p>

<p>--</p>

<p>Another approach, you could get the transform of your DataFrame with df.T and then use the <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.duplicated.html" rel="nofollow">duplicated</a> function.</p>

python pandas dataframe multi-index