<p>您还可以立即对DataFrame对象使用<code>where()</code>方法。可以将条件作为第一个参数提供给此方法。请参见以下示例:</p>
<pre><code>dataset.where(dataset['class']==0)
</code></pre>
<p>它将给出以下输出</p>
<pre><code> f000001 f000002 f000003 ... f000102 f000103 class
0 0.000000 0.000000 0.000000 ... 0.000000 0.080000 0.0
1 0.000000 0.000000 0.000000 ... 0.000000 0.058824 0.0
2 0.000000 0.000000 0.000000 ... 0.000000 0.095238 0.0
3 0.029867 0.000000 0.012769 ... 0.000000 0.085106 0.0
4 0.000000 0.000000 0.000000 ... 0.000000 0.085106 0.0
5 0.000000 0.000000 0.000000 ... 0.000000 0.085106 0.0
6 0.000000 0.000000 0.000000 ... 0.000000 0.127660 0.0
7 0.000000 0.000000 0.000000 ... 0.000000 0.106383 0.0
8 0.000000 0.000000 0.000000 ... 0.000000 0.127660 0.0
9 0.000000 0.000000 0.000000 ... 0.000000 0.106383 0.0
10 0.000000 0.000000 0.000000 ... 0.000000 0.085106 0.0
11 0.021392 0.000000 0.000000 ... 0.000000 0.042553 0.0
12 -0.063880 -0.124403 -0.102466 ... 0.000000 0.042553 0.0
13 0.000000 0.000000 0.000000 ... 0.000000 0.021277 0.0
14 0.000000 0.000000 0.000000 ... 0.000000 0.000000 0.0
15 0.000000 0.000000 -0.060884 ... 0.000000 0.000000 0.0
[18323 rows x 104 columns]
</code></pre>
<p><em>(为了答案的简洁,我去掉了其余的输出)</em></p>
<p>与仅引用相比,使用此方法的一个巨大优势是,您还可以使用<code>other</code>参数替换与条件不匹配的值,并使用<code>inplace</code>参数对与条件匹配的值执行一些操作。基本上,您可以根据需要重建数据帧的行。</p>
<p>此外,由于此函数返回的数据帧减去与条件不匹配的行,因此可以重新引用特定列,如</p>
<pre><code>dataset.where(dataset['class']==0)['f000001']
</code></pre>
<p>这将为您打印<code>'f000001'</code>(第一个特性)列,其中类标签为0。</p>