简体   繁体   English

在熊猫中的dropna()对面

[英]Opposite of dropna() in 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. 我有一个pandas DataFrame ,我希望将其分成观察,其中没有缺失值和缺少值的观察。 I can use dropna() to get rows without missing values. 我可以使用dropna()来获取没有缺少值的行。 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()

Check null by row and filter with boolean indexing: 检查null的行,并与布尔索引筛选:

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

#  col1 col2
#1  NaN  7.0
#2  3.0  NaN

~ = 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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