Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opposite of dropna() in pandas

Tags:

python

pandas

I have a pandas DataFrame that I want to separate into observations for which there are no missing values and observations with missing values. I can use dropna() to get rows without missing values. Is there any analog to get rows with missing values?

#Example DataFrame
import pandas as pd
df = pd.DataFrame({'col1': [1,np.nan,3,4,5],'col2': [6,7,np.nan,9,10],})

#Get observations without missing values
df.dropna()
like image 545
Gaurav Bansal Avatar asked Oct 08 '17 01:10

Gaurav Bansal


People also ask

What is the difference between Dropna and Fillna?

In many cases, you will want to replace missing values in a Pandas DataFrame instead of dropping it completely. The fillna method is designed for this. Pandas has a built-in method called dropna. When applied against a DataFrame, the dropna method will remove any rows that contain a NaN value.

Does Dropna drop NaT?

Pandas Drop All Rows with any Null/NaN/NaT Values This is the default behavior of dropna() function.

How remove all nulls in pandas?

Drop all rows having at least one null value When it comes to dropping null values in pandas DataFrames, pandas. DataFrame. dropna() method is your friend.


2 Answers

Check null by row and filter with boolean indexing:

df[df.isnull().any(1)]

#  col1 col2
#1  NaN  7.0
#2  3.0  NaN
like image 115
Psidom Avatar answered Sep 20 '22 15:09

Psidom


~ = Opposite :-)

df.loc[~df.index.isin(df.dropna().index)]

Out[234]: 
   col1  col2
1   NaN   7.0
2   3.0   NaN

Or

df.loc[df.index.difference(df.dropna().index)]
Out[235]: 
   col1  col2
1   NaN   7.0
2   3.0   NaN
like image 44
BENY Avatar answered Sep 21 '22 15:09

BENY