Python - Check if the Pandas Index only consists of booleans

PythonServer Side ProgrammingProgramming

<p>To check if the Pandas Index only consists of booleans, use the <strong>index.is_boolean()</strong> method in Pandas. At first, import the required libraries -</p><pre class="just-code notranslate language-python3" data-lang="python3">import pandas as pd</pre><p>Creating Pandas index</p><pre class="just-code notranslate language-python3" data-lang="python3">index = pd.Index([True, True, False, False, True, True, True]) </pre><p>Display the Pandas index &minus;</p><pre class="just-code notranslate language-python3" data-lang="python3">print(&quot;Pandas Index... &quot;,index)</pre><p>Check whether index values have only booleans &minus;</p><pre class="just-code notranslate language-python3python" data-lang="python3">print(&quot; Index values only consists of booleans? &quot;,index.is_boolean()) </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 # Creating Pandas index index = pd.Index([True, True, False, False, True, True, True]) # Display the Pandas index print(&quot;Pandas Index... &quot;,index) # Return the number of elements in the Index print(&quot; Number of elements in the index... &quot;,index.size) # Return the dtype of the data print(&quot; The dtype object... &quot;,index.dtype) # check whether index values has only booleans print(&quot; Index values only consists of booleans? &quot;,index.is_boolean())</pre><h2>Output</h2><p>This will produce the following output &minus;</p><pre class="result notranslate">Pandas Index... Index([True, True, False, False, True, True, True], dtype=&#39;object&#39;) Number of elements in the index... 7 The dtype object... object Index values only consists of booleans? True</pre>
raja
Updated on 13-Oct-2021 07:18:34

Advertisements