Python Pandas - Get location and sliced index for requested label/ level but do not drop the level

PythonServer Side ProgrammingProgramming

<p>To get location and sliced index for requested label/ level in a MultiIndex, use the <strong>get_loc_level()</strong> method in Pandas. Use the <strong>drop_level</strong> parameter and set it <strong>False</strong> to avoid dropping the level.</p><p>At first, import the required libraries &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">import pandas as pd</pre><p>MultiIndex is a multi-level, or hierarchical, index object for pandas objects &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">multiIndex = pd.MultiIndex.from_arrays([list(&#39;pqrrss&#39;), list(&#39;strvwx&#39;)],names=[&#39;One&#39;, &#39;Two&#39;])</pre><p>Display the MultiIndex &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">print(&quot;The MultiIndex... &quot;,multiIndex) </pre><p>Get the location and sliced index. To avoid dropping a level, we have used the &quot;drop_level&quot; parameter with value &quot;False&quot; &minus;</p><pre class="just-code notranslate language-python3python" data-lang="python3">print(&quot; Get the location and sliced index (avoid dropping the level)... &quot;,multiIndex.get_loc_level(&#39;r&#39;, drop_level=False))</pre><h2>Example</h2><p>Following is the code &minus;</p><pre class="demo-code notranslate language-python3" data-lang="python3">import pandas as pd # MultiIndex is a multi-level, or hierarchical, index object for pandas objects multiIndex = pd.MultiIndex.from_arrays([list(&#39;pqrrss&#39;), list(&#39;strvwx&#39;)],names=[&#39;One&#39;, &#39;Two&#39;]) # display the MultiIndex print(&quot;The MultiIndex... &quot;,multiIndex) # get the levels in MultiIndex print(&quot; The levels in MultiIndex... &quot;,multiIndex.levels) # Get the location and sliced index # To avoid dropping a level, we have used the &quot;drop_level&quot; parameter with value &quot;False&quot; print(&quot; Get the location and sliced index (avoid dropping the level)... &quot;,multiIndex.get_loc_level(&#39;r&#39;, drop_level=False))</pre><h2>Output</h2><p>This will produce the following output &minus;</p><pre class="result notranslate">The MultiIndex... MultiIndex([(&#39;p&#39;, &#39;s&#39;), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#39;q&#39;, &#39;t&#39;), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#39;r&#39;, &#39;r&#39;), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#39;r&#39;, &#39;v&#39;), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#39;s&#39;, &#39;w&#39;), &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (&#39;s&#39;, &#39;x&#39;)], &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; names=[&#39;One&#39;, &#39;Two&#39;]) The levels in MultiIndex... &nbsp; &nbsp;[[&#39;p&#39;, &#39;q&#39;, &#39;r&#39;, &#39;s&#39;], [&#39;r&#39;, &#39;s&#39;, &#39;t&#39;, &#39;v&#39;, &#39;w&#39;, &#39;x&#39;]] Get the location and sliced index (avoid dropping the level)... (slice(2, 4, None), MultiIndex([(&#39;r&#39;, &#39;r&#39;),(&#39;r&#39;, &#39;v&#39;)],names=[&#39;One&#39;, &#39;Two&#39;]))</pre>
raja
Updated on 19-Oct-2021 08:19:02

Advertisements