Using IndexSlice to filter MultiIndex Dataframes with Pandas
<p><strong>Question:</strong> How do I filter the rows such that I return only the rows where the injection is not equal to 0 or NaN and not lose the values of the other columns?</p>

<p>I have a dataframe created with the following code:</p>

<pre><code>import pandas as pd

df=pd.DataFrame(
               [
               [5777, 100, 5385, 200, 5419, 4887, 100, 200],
               [4849, 0, 4539, 0, 3381, 0, 0, ],
               [4971, 0, 3824, 0, 4645, 3424, 0, 0, ],
               [4827, 200, 3459, 300, 4552, 3153, 100, 200, ],
               [5207, 0, 3670, 0, 4876, 3358, 0, 0, ],
               ],
               index=pd.to_datetime(['2010-01-01',
                                     '2010-01-02',
                                     '2010-01-03',
                                     '2010-01-04',
                                     '2010-01-05']),
               columns=pd.MultiIndex.from_tuples(
                                                [('Portfolio A', 'GBP', 'amount'),
                                                 ('Portfolio A', 'GBP', 'injection'),
                                                 ('Portfolio B', 'EUR', 'amount'),                                           ('Portfolio B', 'EUR', 'injection'),
                                                 ('Portfolio C', 'USD', 'amount'),                                           ('Portfolio C', 'USD', 'injection'),
                                                 ('Portfolio D', 'JPY', 'amount'),                                           ('Portfolio D', 'JPY', 'injection')])
                                   ).sortlevel(axis=1)
</code></pre>

<p><strong>Next I can create a DataFrame with the slice of the data (in this instance it's all the data)</strong></p>

<pre><code>df1=df.loc[pd.IndexSlice[:], pd.IndexSlice[:,:, ['amount', 'injection']]]
</code></pre>

<p><strong>Next create a new DataFrame where injection is != 0</strong></p>

<pre><code>df2=df1[df1.loc[pd.IndexSlice[:], pd.IndexSlice[:, :, 'injection']]!=0]
</code></pre>

<p><strong>Question:</strong> Why does this reset all values in 'amount' column to NaN?</p>

<p>The next step once the amounts are available is to remove rows with all NaN</p>

<pre><code>df3=df2.dropna(axis=0, how='all', thresh=None, subset=None, inplace=False)
</code></pre>

<p><strong>The desired output is all data across row indexes:</strong></p>

<pre><code>2010-01-01
2010-01-03
2010-01-04
2010-01-05
</code></pre>