.apply(pd.to_numeric) returns error message
<p>I have a dataframe with two columns I want to convert to numeric type.  I use the following code:</p>

<pre><code>df[["GP","G"]]=df[["GP","G"]].apply(pd.to_numeric)
</code></pre>

<p>Python returns the following error message:</p>

<pre><code>File "C:\Users\Alexandros_7\Anaconda3\lib\site-packages\pandas\core\frame.py", line 4157, in _apply_standard
results[i] = func(v)

File "C:\Users\Alexandros_7\Anaconda3\lib\site-packages\pandas\tools\util.py", line 115, in to_numeric
coerce_numeric=coerce_numeric)

File "pandas\src\inference.pyx", line 612, in pandas.lib.maybe_convert_numeric (pandas\lib.c:53558)

File "pandas\src\inference.pyx", line 598, in      pandas.lib.maybe_convert_numeric (pandas\lib.c:53344)

ValueError: ('Unable to parse string', 'occurred at index GP')
</code></pre>

<p>How can I fix this problem?  How can I convert multiple column types at once with a command?  Thank you!</p>

<p>Your code will only work if all the data can be parsed to numeric. </p>

<p>If not, there is at least one value in dataframe which is not convertible to numeric. You can use <code>errors</code> parameter according to your choice in such case. Here is an example.</p>

<pre><code>&gt;&gt;&gt; df = pd.DataFrame({'A' : list('aabbcd'), 'B' : list('ffghhe')})

&gt;&gt;&gt; df
   A  B
0  a  f
1  a  f
2  b  g
3  b  h
4  c  h
5  d  e

&gt;&gt;&gt; df.apply(pd.to_numeric, errors='ignore')
   A  B
0  a  f
1  a  f
2  b  g
3  b  h
4  c  h
5  d  e

&gt;&gt;&gt; df.apply(pd.to_numeric, errors='coerce')
    A   B
0 NaN NaN
1 NaN NaN
2 NaN NaN
3 NaN NaN
4 NaN NaN
5 NaN NaN

&gt;&gt;&gt; df.apply(pd.to_numeric, errors='raise')
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 4042, in apply
    return self._apply_standard(f, axis, reduce=reduce)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 4138, in _apply_standard
    results[i] = func(v)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 4020, in f
    return func(x, *args, **kwds)
  File "/usr/local/lib/python2.7/dist-packages/pandas/tools/util.py", line 98, in to_numeric
    coerce_numeric=coerce_numeric)
  File "pandas/src/inference.pyx", line 612, in pandas.lib.maybe_convert_numeric (pandas/lib.c:53932)
  File "pandas/src/inference.pyx", line 598, in pandas.lib.maybe_convert_numeric (pandas/lib.c:53719)
ValueError: ('Unable to parse string', u'occurred at index A')
&gt;&gt;&gt; 
</code></pre>

<p>Here is the documentation for <code>errors</code></p>

<blockquote>
  <p>errors : {âignoreâ, âraiseâ, âcoerceâ}, default âraiseâ</p>
  
  <p>If âraiseâ, then invalid parsing will raise an exception </p>
  
  <p>If âcoerceâ,then invalid parsing will be set as NaN </p>
  
  <p>If âignoreâ, then invalid parsing will return the input</p>
</blockquote>

python