擅长:python、mysql、java
<p>这个街区:</p>
<pre><code>if (df['NA'] > 1).any():
print(True)
elif (df['NA'] == 1).any():
print(False)
</code></pre>
<p>将始终打印True,因为列“NA”的值大于1,因此不会计算第二个elif。基于您的评论,我认为您希望遍历数据帧并评估每个元素(如果我没有弄错的话)。你可以这样做:</p>
<pre><code>for val in df['NA']:
if val > 1:
print(True)
elif val == 1:
print(False)
</code></pre>
<p>这将评估列'NA'的每个元素,对于您的实际用例,您需要知道给定val的索引,这可以使用<code>enumerate</code>来完成。例如:</p>
<pre><code>for (idx, val) in enumerate(df['NA']):
if val > 1:
d5 = -A1 * df['NOB'].iloc[idx]
elif val == 1:
d5 = E * df['NOB'].iloc[idx]
</code></pre>
<p><code>idx</code>是当前元素的索引,您可以使用<code>iloc</code>访问该索引处其他列的元素。我希望这有帮助,祝你好运。你知道吗</p>