我正在尝试使用 Pandas 清理
一个非常大的数据框。
数据集包含重复的指标列,例如高度、体重、性别和年龄。一些行包含列名 currentAge
的数据,而其他行包含列名 currentAge2
的数据。
因此,例如,我想删除 currentAge
和 currentAge2
中具有 NaN
的行,因为它们是无用的数据点。我想对所有其他指标执行相同的操作。
我的数据框的索引从0开始。下面是我试过的代码。
for index, row in csv.iterrows():
if ((math.isnan(row['currentAge']) and math.isnan(row['currentAge2'])) == True):
csv.drop(csv.index[index])
这不起作用,当我使用 in place=True 时,我得到一个索引越界错误。如果有人能阐明我如何正确清理这个数据框,那就太好了。 csv 是我的数据框的名称。
最佳答案
我认为这里不需要iterrows
。
csv[~(csv['currentAge'].isnull())&(csv['currentAge2'].isnull())]
关于python - 在保留索引的同时删除包含 NaN 的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50960537/