<p>您可以通过调用<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.convert_objects.html#pandas.DataFrame.convert_objects" rel="noreferrer">^{<cd1>}</a>来转换大多数列:</p>
<pre><code>In [36]:
df = df.convert_objects(convert_numeric=True)
df.dtypes
Out[36]:
Date object
WD int64
Manpower float64
2nd object
CTR object
2ndU float64
T1 int64
T2 int64
T3 int64
T4 float64
dtype: object
</code></pre>
<p>对于列“2nd”和“CTR”,我们可以调用矢量化的<a href="http://pandas.pydata.org/pandas-docs/stable/api.html#string-handling" rel="noreferrer">^{<cd2>}</a>方法来替换千位分隔符并删除“%”符号,然后<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.astype.html#pandas.Series.astype" rel="noreferrer">^{<cd3>}</a>来转换:</p>
<pre><code>In [39]:
df['2nd'] = df['2nd'].str.replace(',','').astype(int)
df['CTR'] = df['CTR'].str.replace('%','').astype(np.float64)
df.dtypes
Out[39]:
Date object
WD int64
Manpower float64
2nd int32
CTR float64
2ndU float64
T1 int64
T2 int64
T3 int64
T4 object
dtype: object
In [40]:
df.head()
Out[40]:
Date WD Manpower 2nd CTR 2ndU T1 T2 T3 T4
0 2013/4/6 6 NaN 2645 5.27 0.29 407 533 454 368
1 2013/4/7 7 NaN 2118 5.89 0.31 257 659 583 369
2 2013/4/13 6 NaN 2470 5.38 0.29 354 531 473 383
3 2013/4/14 7 NaN 2033 6.77 0.37 396 748 681 458
4 2013/4/20 6 NaN 2690 5.38 0.29 361 528 541 381
</code></pre>
<p>或者可以在不调用<code>astype</code>的情况下执行上面的字符串处理操作,然后调用<code>convert_objects</code>一次性转换所有内容。</p>
<p><strong>更新</strong></p>
<p>由于版本<code>0.17.0</code><code>convert_objects</code>已被弃用,并且没有顶级函数来执行此操作,因此您需要执行以下操作:</p>
<p><code>df.apply(lambda col:pd.to_numeric(col, errors='coerce'))</code></p>
<p>请参阅<a href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_numeric.html" rel="noreferrer">docs</a>和相关问题:<a href="https://stackoverflow.com/questions/36814100/pandas-to-numeric-for-multiple-columns">pandas: to_numeric for multiple columns</a></p>