【发布时间】:2019-03-17 03:44:19
【问题描述】:
我正在尝试删除 Pandas 数据框中两列中的任何一列都不为零的所有行。我的数据框索引从 0 到 620。这是我的代码:
for index in range(0, 621):
if((zeroes[index,1] != 0) and (zeroes[index,3] != 0)):
del(zeroes[index,])
我不断收到一个关键错误。 KeyError: (0, 1)
我的导师建议我更改范围以测试我的数据框中是否有坏行。我做到了。我检查了数据框的尾部,然后将范围更改为 (616, 621)。然后我得到了关键错误:(616, 1)。
有谁知道我的代码有什么问题或为什么我会遇到关键错误?
此代码还会产生 (0,1) 的关键错误:
index = 0
while (index < 621):
if((zeroes[index,1] != 0) and (zeroes[index,3] != 0)):
del(zeroes[index,])
index = index + 1
【问题讨论】:
-
请始终提供一个最小的、完整的、可验证的代码示例。问题显然出在
zeroes,但如果不知道zeroes是什么,我们将无法帮助您。 -
zeroes 是我的数据框的名称。它包含密歇根州各县的平均考试成绩数据。
标签: python pandas for-loop dataframe range