擅长:python、mysql、java
<p>要识别<code>NaN</code>值,请使用<a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#boolean-indexing" rel="noreferrer">^{<cd2>}</a>:</p>
<pre><code>print(df[df['x'].isnull()])
</code></pre>
<p>然后,对于删除所有非数值,请将<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.to_numeric.html" rel="noreferrer">^{<cd3>}</a>与参数<code>errors='coerce'</code>一起使用-它将非数值替换为<code>NaN</code>:</p>
<pre><code>df['x'] = pd.to_numeric(df['x'], errors='coerce')
</code></pre>
<p>若要删除列<code>x</code>中具有<code>NaN</code>s的所有行,请使用<a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.dropna.html" rel="noreferrer">^{<cd8>}</a>:</p>
<pre><code>df = df.dropna(subset=['x'])
</code></pre>
<p>上次将值转换为<code>int</code>s:</p>
<pre><code>df['x'] = df['x'].astype(int)
</code></pre>