Getting column values from multi index data frame pandas
<p>I have a multi index data frame shown below:</p>

<pre><code>    1                2

panning  sec        panning     sec

 None    5.0        None        0.0
 None    6.0        None        1.0
Panning  7.0        None        2.0 
 None    8.0        Panning     3.0
 None    9.0        None        4.0
 Panning  10.0      None        5.0
</code></pre>

<p>I am iterating over the rows and getting the index wherever there is a value 'panning' in the panning column by</p>

<pre><code> ide=[]
 for index,row in dfs.iterrows():
        if [row[:, 'Panning'][row[:, 'Panning'] == 'Panning']]:
               ide.append(row[:, 'Panning'][row[:, 'Panning'] == 'Panning'].index.tolist())

print ide
</code></pre>

<p>If I execute the above code I get the output </p>

<pre><code>[[],[],[1],[2],[],[1]]
</code></pre>

<p>which represents the index where the value is panning</p>

<p>Now, I also want to get the corresponding sec value also like, for example for row 3 for value panning I would like to get sec value 7.0 along with index 1. I would like O\P to be</p>

<pre><code>[[],[],[1,7.0],[2,3.0],[],[1,10]]
</code></pre>

<p>Basically I need the O/P as combination of the index where the value is panning and the subsequent value in the seconds column.</p>

<p>consider the <code>pd.DataFrame</code> <code>df</code> in the setup reference below</p>

<p><strong><em>method 1</em></strong>  </p>

<ul>
<li><code>xs</code> for cross section</li>
<li><code>any(1)</code> to check if any in row</li>
</ul>

<hr>

<pre><code>df.loc[df.xs('Panning', axis=1, level=1).eq('Panning').any(1)]
</code></pre>

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

<p><strong><em>method 2</em></strong>  </p>

<ul>
<li><code>stack</code></li>
<li><code>query</code></li>
<li><code>unstack</code></li>
</ul>

<hr>

<pre><code>df.stack(0).query('Panning == "Panning"').stack().unstack([-2, -1])
</code></pre>

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

<hr>

<p>To return just the <code>sec</code> columns</p>

<pre><code>df.xs('sec', axis=1, level=1)[df.xs('Panning', axis=1, level=1).eq('Panning').any(1)]
</code></pre>

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

<p><strong><em>setup</em></strong><br>
Reference</p>

<pre><code>from StringIO import StringIO
import pandas as pd

txt = """None    5.0        None        0.0
None    6.0        None        1.0
Panning  7.0        None        2.0 
None    8.0        Panning     3.0
None    9.0        None        4.0
Panning  10.0      None        5.0"""

df = pd.read_csv(StringIO(txt), delim_whitespace=True, header=None)

df.columns = pd.MultiIndex.from_product([[1, 2], ['Panning', 'sec']])
df
</code></pre>

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

python pandas