Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove row with all NaN from DataFrame in pandas

Tags:

python

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
like image 905
Shahriar Avatar asked Dec 20 '14 10:12

Shahriar


1 Answers

You could use dropna:

In [13]: df2[df1 != df2].dropna(how='all')
Out[13]: 
   col1  col2
0     4     6
2     5     5
like image 81
unutbu Avatar answered Sep 28 '22 00:09

unutbu