擅长:python、mysql、java
<p>您可以尝试使用布尔索引:</p>
<pre><code>df[df['student_1'] & df['student_2'] & (df.sum(1) == 2)]
</code></pre>
<p>输出:</p>
<pre><code> student_1 student_2 student_3 student_4
classroom_id
2 True True False False
</code></pre>
<p>而且,要获得计数,可以使用<code>shape</code>:</p>
<pre><code>df[df['student_1'] & df['student_2'] & (df.sum(1) == 2)].shape[0]
</code></pre>
<p>输出:</p>
<pre><code>1
</code></pre>