How do I replace all values equal to x in nth level of a multi index
<p>Consider the <code>pd.Series</code> <code>s</code></p>

<pre><code>s = pd.Series(np.arange(6),
              pd.MultiIndex.from_product([[1, 2], [1, 2, 3]]))

1  1    0
   2    1
   3    2
2  1    3
   2    4
   3    5
dtype: int64
</code></pre>

<p>I want to replace all values of <code>3</code> in the index with <code>4</code>.</p>

<hr>

<p>I just came up with this and I don't like it.</p>

<pre><code>lvl0 = s.index.get_level_values(0)
lvl1 = s.index.get_level_values(1)

s.index = pd.MultiIndex.from_tuples(zip(lvl0, np.where(lvl1 == 3, 4, lvl1)))
s

1  1    0
   2    1
   4    2
2  1    3
   2    4
   4    5
dtype: int64
</code></pre>

<p>You could use <code>s.index.levels = [[1, 2], [1, 2, 4]]</code> but you may get a FutureWarning:</p>

<blockquote>
  <p>FutureWarning: setting `levels` directly is deprecated. Use set_levels
  instead</p>
</blockquote>

<p>So you may try</p>

<pre><code>&gt;&gt;&gt; s.index = s.index.set_levels([[1, 2], [1, 2, 4]])
&gt;&gt;&gt; s
  1  1    0
     2    1
     4    2
  2  1    3
     2    4
     4    5
</code></pre>

python pandas numpy multi-index