<p>在你的例子中,我会采用稍微不同的设计。您可以这样做:</p>
<pre><code>df['area1'] = np.where((df.Inches <= 60) & (df.Weight <= 150),1,0)
df['group_a'] = np.where((df.Siblings >= 2) & (df.Twin == 'Y'),1,0)
df['group_b'] = np.where((df.Siblings >= 2) & (df.Twin == 'N'),1,0)
df['group_c'] = np.where((df.Siblings == 1) & (df.Twin == 'Y'),1,0)
df['group_d'] = np.where((df.Siblings == 1) & (df.Twin == 'N'),1,0)
</code></pre>
<p>结果是这样的:</p>
<p><a href="https://i.stack.imgur.com/uf7rv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uf7rv.png" alt="enter image description here"/></a></p>
<p>从这一点开始,您可以构建查询,以便查看组\u b:</p>
<pre><code>df.groupby(['area1'])['group_b'].sum()[1]
</code></pre>
<p>你会得到你想要的结果:1。你可以玩总和或计数来调整你的桌子。你知道吗</p>
<p>最后:</p>
<pre><code>for col in df.columns[6:]:
r = df.groupby(['area1'])[col].sum()[1]
print ("in area1 there are",r,'children in',col)
</code></pre>
<p>将产生:</p>
<pre><code>in area1 there are 2 children in group_a
in area1 there are 1 children in group_b
in area1 there are 0 children in group_c
in area1 there are 1 children in group_d
</code></pre>