Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在for循环中有条件地删除行_Python_Pandas_For Loop - Fatal编程技术网

Python 在for循环中有条件地删除行

Python 在for循环中有条件地删除行,python,pandas,for-loop,Python,Pandas,For Loop,我的熊猫数据帧有17543行。我想删除一行,前提是每列都包含“nan”。我根据链接尝试了说明 但是没有帮助。下面是我的代码 NullRows=0 for i in range(len(SetMerge.index)): if(SetMerge.iloc[i].isnull().all()): df=SetMerge.drop(SetMerge.index[i]) NullRows +=1 print("total null rows : ", NullR

我的熊猫数据帧有17543行。我想删除一行,前提是每列都包含“nan”。我根据链接尝试了说明 但是没有帮助。下面是我的代码

NullRows=0
for i in range(len(SetMerge.index)):
    if(SetMerge.iloc[i].isnull().all()):
        df=SetMerge.drop(SetMerge.index[i])
        NullRows +=1

print("total null rows : ", NullRows)

我在df中只删除了一行17542行,而空行输出是30行。

drop
不会改变您的
SetMerge
。因此,您需要在
drop
之后重新分配
SetMerge
,或者使用另一个函数。
这是写在回答,通过链接,你已经张贴在这里,并检查。为突变指定inplace=True选项。

为什么不使用dataframe.dropna(how='all')?换句话说,您不应该在allimprovided readabilityAnna上使用循环,在这里,不要使用for循环。