Convert string column to integer
<p>I have a dataframe like below</p>

<pre><code>    a   b
0   1   26190
1   5   python
2   5   580
</code></pre>

<p>I want to make column <code>b</code> to host only integers, but as you can see <code>python</code> is not int convertible, so I want to delete the row at index <code>1</code>. My expected out put has to be like</p>

<pre><code>    a   b
0   1   26190
1   5   580
</code></pre>

<p>How to filter and remove using pandas in python?</p>

<p>You can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_numeric.html" rel="nofollow"><code>to_numeric</code></a> with <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.notnull.html" rel="nofollow"><code>notnull</code></a> and filter by <a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing" rel="nofollow"><code>boolean indexing</code></a>:</p>

<pre><code>print (pd.to_numeric(df.b, errors='coerce'))
0    26190.0
1        NaN
2      580.0
Name: b, dtype: float64

print (pd.to_numeric(df.b, errors='coerce').notnull())
0     True
1    False
2     True
Name: b, dtype: bool

df = df[pd.to_numeric(df.b, errors='coerce').notnull()]
print (df)

   a      b
0  1  26190
2  5    580
</code></pre>

<p>Another solution by comment of <a href="http://stackoverflow.com/questions/39694192/convert-string-column-to-integer/39694974#comment66688077_39694192">Boud</a> - use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_numeric.html" rel="nofollow"><code>to_numeric</code></a> with <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.dropna.html" rel="nofollow"><code>dropna</code></a> and last convert to <code>int</code> by <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.astype.html" rel="nofollow"><code>astype</code></a>:</p>

<pre><code>df.b = pd.to_numeric(df.b, errors='coerce')
df = df.dropna(subset=['b'])
df.b = df.b. astype(int)
print (df)
   a      b
0  1  26190
2  5    580
</code></pre>

<hr>

<p>If need check all rows with bad data use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.isnull.html" rel="nofollow"><code>isnull</code></a> - filter all data where after applying function <code>to_numeric</code> get <code>NaN</code>:</p>

<pre><code>print (pd.to_numeric(df.b, errors='coerce').isnull())
0    False
1     True
2    False
Name: b, dtype: bool

print (df[pd.to_numeric(df.b, errors='coerce').isnull()])
   a       b
1  5  python
</code></pre>

python string pandas numpy int