从 Pandas 的 DataFrame 中删除包含所有 NaN 的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27579285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:46:41  来源:igfitidea点击:

Remove row with all NaN from DataFrame in pandas

pythonpandas

提问by Mir Shahriar Sabuj

I have two data frame df1, df2, which I want to combine to the new dataframe df. This however creates an row with all NaN:

我有两个数据框df1df2我想将它们组合到新的数据框df。然而,这会创建一个包含所有 NaN 的行:

>>> from pandas import  DataFrame
>>> df1 = DataFrame({'col1':[1,2,3], 'col2':[2,3,4]})
>>> df2 = DataFrame({'col1':[4,2,5], 'col2':[6,3,5]})

>>> df = df2[~df2.isin(df1)]
>>> df
   col1  col2
0     4     6
1   NaN   NaN
2     5     5

How can I remove this row, such that dflooks like:

如何删除这一行,df如下所示:

>>> df
   col1  col2
0     4     6
2     5     5

回答by unutbu

You could use dropna:

你可以使用dropna

In [13]: df2[df1 != df2].dropna(how='all')
Out[13]: 
   col1  col2
0     4     6
2     5     5

回答by Mir Shahriar Sabuj

>>> df = df2[~df2.isin(df1).all(1)]
>>> df
    col1  col2
0     4     6
2     5     5