Multi Indexing and masks with logic pandas
<p>I have 4 indexes. Mun, loc, geo and block. And I need to create masks to operate with them so I can create masks and perform operations that will look like this:</p>

<pre><code>                       data1  data2
mun  loc  geo  block
0    0    0    0       12     12
1    0    0    0       20     20
1    1    0    0       10     10
1    1    1    0       10     10   
1    1    1    1       3      3/4
1    1    1    2       4      4/4
1    1    2    0       30     30   
1    1    2    1       1      1/3
1    1    2    2       3      3/3
1    1    0    0       4      4
1    2    1    1       10     10/12
1    2    1    2       12     12/12
2    0    0    0       60     60
2    1    1    1       123    123/123
2    1    1    2       7      7/123
2    1    2    1       6      6/6
2    1    2    2       1      1/6
</code></pre>



<pre><code>                       data1  data2
mun  loc  geo  block
0    0    0    0       12     12
1    0    0    0       20     20
1    1    0    0       10     10
1    1    1    0       10     10/30   
1    1    1    1       4      4
1    1    2    0       30     30/30   
1    2    1    0       2      2/3
1    2    2    0       3      3/3
1    2    3    0       1      1/3
2    0    0    0       60     60
2    1    1    0       12     12/88 
2    1    1    1       1       1
2    1    2    0       88     88/88
2    1    2    1       9      9
</code></pre>



<pre><code>                       data1  data2
mun  loc  geo  block
0    0    0    0       14     14
1    0    0    0       12     12
1    1    0    0       20     20/20
1    1    1    0       10     10   
1    1    1    1       31     31
1    2    0    0       15     15/20 
1    2    1    1       11     11
2    0    0    0       80     80
2    1    0    0       100    100/100
2    1    1    2       7      7
2    2    0    0       11     11/100

                       data1  data2
mun  loc  geo  block
0    0    0    0       55     55
1    0    0    0       70     70/70
1    1    0    0       12     12
1    1    1    0       13     13   
2    0    0    0       60     60/70
2    1    1    1       12     12
2    1    2    1       6      6
3    0    0    0       12     12/70
</code></pre>

<p>That is, take the max value inside the hierarchy and divide each element by it. I got help in <a href="http://stackoverflow.com/q/40012740/2901002">another question</a> regarding the first problem, but I'm having a lot of problems getting grasp of multi index. Any help will me appreciated.</p>

<p>It was not easy. But mainly use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Index.get_level_values.html" rel="nofollow"><code>get_level_values</code></a> for select values for condition:</p>

<p>Level <strong><code>block</code></strong>:</p>

<pre><code>print (df)
                   data1    data2
mun loc geo block                
0   0   0   0         12       12
1   0   0   0         20       20
    1   0   0         10       10
        1   0         10       10
            1          3      3/4
            2          4      4/4
        2   0         30       30
            1          1      1/3
            2          3      3/3
        0   0          4        4
    2   1   1         10    10/12
            2         12    12/12
2   0   0   0         60       60
    1   1   1        123  123/123
            2          7    7/123
        2   1          6      6/6
            2          1      1/6

mask3 =  (df.index.get_level_values('mun') != 0) &amp; \
         (df.index.get_level_values('loc') != 0 ) &amp; \
         (df.index.get_level_values('geo') != 0) &amp; \
         (df.index.get_level_values('block') != 0 )

print (mask3)
[False False False False  True  True False  True  True False  True  True
 False  True  True  True  True]

df2 = df.ix[mask3, 'data1'].groupby(level=['mun','loc','geo']).max()
#print (df2)

df2 = df2.reindex(df.reset_index(level=3, drop=True).index).mask(~mask3).fillna(1)
#print (df2)
</code></pre>



<pre><code>print (df['data1'].div(df2.values,axis=0))
mun  loc  geo  block
0    0    0    0        12.000000
1    0    0    0        20.000000
     1    0    0        10.000000
          1    0        10.000000
               1         0.750000
               2         1.000000
          2    0        30.000000
               1         0.333333
               2         1.000000
          0    0         4.000000
     2    1    1         0.833333
               2         1.000000
2    0    0    0        60.000000
     1    1    1         1.000000
               2         0.056911
          2    1         1.000000
               2         0.166667
dtype: float64
</code></pre>

<hr>

<p>Level <strong><code>geo</code></strong>:</p>

<pre><code>print (df)
                   data1  data2
mun loc geo block              
0   0   0   0         12     12
1   0   0   0         20     20
    1   0   0         10     10
        1   0         10  10/30
            1          4      4
        2   0         30  30/30
    2   1   0          2    2/3
        2   0          3    3/3
        3   0          1    1/3
2   0   0   0         60     60
    1   1   0         12  12/88
            1          1      1
        2   0         88  88/88
            1          9      9

df1 = df.reset_index(drop=True, level='block')

mask3 =  (df.index.get_level_values('mun') != 0) &amp; \
             (df.index.get_level_values('loc') != 0 ) &amp; \
             (df.index.get_level_values('geo') != 0) &amp; \
             (df.index.get_level_values('block') == 0 )

print (mask3)
[False False False  True False  True  True  True  True False  True False
  True False]

df2 = df1.ix[mask3, 'data1'].groupby(level=['mun','loc']).max()

df2=df2.reindex(df.reset_index(level=['geo','block'], drop=True).index).mask(~mask3).fillna(1)
print (df2)
df['new'] = df['data1'].div(df2.values,axis=0)
</code></pre>



<pre><code>print (df)
                   data1  data2        new
mun loc geo block                         
0   0   0   0         12     12  12.000000
1   0   0   0         20     20  20.000000
    1   0   0         10     10  10.000000
        1   0         10  10/30   0.333333
            1          4      4   4.000000
        2   0         30  30/30   1.000000
    2   1   0          2    2/3   0.666667
        2   0          3    3/3   1.000000
        3   0          1    1/3   0.333333
2   0   0   0         60     60  60.000000
    1   1   0         12  12/88   0.136364
            1          1      1   1.000000
        2   0         88  88/88   1.000000
            1          9      9   9.000000
</code></pre>

<hr>

<p>Level <strong><code>loc</code></strong>:</p>

<pre><code>print (df)
                   data1    data2
mun loc geo block                
0   0   0   0         14       14
1   0   0   0         12       12
    1   0   0         20    20/20
        1   0         10       10
            1         31       31
    2   0   0         15    15/20
        1   1         11       11
2   0   0   0         80       80
    1   0   0        100  100/100
        1   2          7        7
    2   0   0         11   11/100

df1 = df.reset_index(drop=True, level=['block', 'geo'])


mask3 =  (df.index.get_level_values('mun') != 0) &amp; \
         (df.index.get_level_values('loc') != 0 ) &amp; \
         (df.index.get_level_values('geo') == 0) &amp; \
         (df.index.get_level_values('block') == 0 )

print (mask3)
[False False  True False False  True False False  True False  True]

df2 = df1.ix[mask3, 'data1'].groupby(level=['mun']).max()
#print (df2)

df2 =df2.reindex(df.reset_index(level=['geo','block', 'loc'], drop=True).index).mask(~mask3).fillna(1)
#print (df2)
</code></pre>



<pre><code>print (df['data1'].div(df2.values,axis=0))
mun  loc  geo  block
0    0    0    0        14.00
1    0    0    0        12.00
     1    0    0         1.00
          1    0        10.00
               1        31.00
     2    0    0         0.75
          1    1        11.00
2    0    0    0        80.00
     1    0    0         1.00
          1    2         7.00
     2    0    0         0.11
dtype: float64
</code></pre>

<hr>

<p>Level <strong><code>mun</code></strong>:</p>

<pre><code>print (df)
                   data1  data2
mun loc geo block              
0   0   0   0         55     55
1   0   0   0         70  70/70
    1   0   0         12     12
        1   0         13     13
2   0   0   0         60  60/70
    1   1   1         12     12
        2   1          6      6
3   0   0   0         12  12/70

mask3 =  (df.index.get_level_values('mun') != 0) &amp; \
         (df.index.get_level_values('loc') == 0 ) &amp; \
         (df.index.get_level_values('geo') == 0) &amp; \
         (df.index.get_level_values('block') == 0 )

print (mask3)
[False  True False False  True False False  True]

df2 = df.ix[mask3, 'data1'].max()
#print (df2)

df2 = pd.Series(df2, index=df.index).mask(~mask3).fillna(1)
#print (df2)
</code></pre>



<pre><code>print (df['data1'].div(df2.values,axis=0))
mun  loc  geo  block
0    0    0    0        55.000000
1    0    0    0         1.000000
     1    0    0        12.000000
          1    0        13.000000
2    0    0    0         0.857143
     1    1    1        12.000000
          2    1         6.000000
3    0    0    0         0.171429
dtype: float64
</code></pre>

python pandas indexing division multi-index