Select row from a DataFrame based on the type of the object(i.e. str)
<p>So there's a DataFrame say:</p>

<pre><code>&gt;&gt;&gt; df = pd.DataFrame({
...                 'A':[1,2,'Three',4],
...                 'B':[1,'Two',3,4]})
&gt;&gt;&gt; df
       A    B
0      1    1
1      2  Two
2  Three    3
3      4    4
</code></pre>

<p>I want to select the rows whose datatype of particular row of a particular column is of type <code>str</code>.</p>

<p>For example I want to select the row where <code>type</code> of data in the column <code>A</code> is a <code>str</code>.
 so it should print something like:</p>

<pre><code>   A      B
2  Three  3
</code></pre>

<p>Whose intuitive code would be like:</p>

<pre><code>df[type(df.A) == str]
</code></pre>

<p>Which obviously doesn't works!</p>

<p>Thanks please help!</p>

<p>You can do something <em>similar</em> to what you're asking with</p>

<pre><code>In [14]: df[pd.to_numeric(df.A, errors='coerce').isnull()]
Out[14]: 
       A  B
2  Three  3
</code></pre>

<p>Why only similar? Because Pandas stores things in homogeneous columns (all entries in a column are of the same type). Even though you constructed the DataFrame from heterogeneous types, they are all made into columns each of the lowest common denominator:</p>

<pre><code>In [16]: df.A.dtype
Out[16]: dtype('O')
</code></pre>

<p>Consequently, you can't ask which rows are of what type - they will all be of the same type. What you <em>can</em> do is to try to convert the entries to numbers, and check where the conversion failed (this is what the code above does).</p>

python pandas