简体   繁体   中英

Remove row with all NaN from DataFrame in pandas

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:

>>> 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 df looks like:

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

You could use dropna :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM