擅长:python、mysql、java
<pre><code>randNum=random.choices(range(-1,2),k=333) # k=3*int
df=pd.DataFrame(np.array(randNum).reshape(-1,3))
# `D2` represents the second diagonal (init to -100 to handle last two rows)
# `D3` represents the third diagonal (init to -100 to handle last two rows)
df['D2'] = df['D3'] = -100
df['D2'][0:-1] = df[1][1:]
df['D3'][0:-2] = df[2][2:]
df[(df[0] != -1) & (df[1]!= -1) & (df[2] != -1) & (df[0] + df['D2'] + df['D3'] == 3)]
</code></pre>
<p>逻辑:</p>
<ol>
<li>对于任何一行,第二个对角线值总是在第1列(D2)中</li>
<li>对于任何一行,第三个对角线值总是在第2列(D3)中</li>
<li>现在可以归结为检查<code>[0] + [D2] + [D3] == 3</code></li>
</ol>