我有一个data frame
,我想做一个null check
,并将null value rows
存储在单独的数据帧中。但是,一个限制是,我不想对一个特定的列进行null检查。我怎样才能做到这一点?
例如,在下面的数据帧中,
Name Age Class
0 tom 10 NaN
1 nick 15 10
2 juli 14 9
如果我申请,df[df.isnull().any(axis=1)]
,它会给我
Name Age Class
0 tom 10 NaN
但是,我不想对Class
列进行空检查,并且我希望在这种情况下有空的数据帧。我在SO中搜索过,但找不到解决方案。
尝试:
cols_to_excl = ['Class']
df.loc[df[df.columns ^ cols_to_excl].isnull().any(axis=1)]
本质上:df.columns ^ cols_to_excl
将返回除列表cols_to_excl
中的所有列之外的所有列。
您可以尝试以下操作:
df[df.iloc[:, :2].isnull().any(axis=1)]
如果你知道你的函数只在这个数据帧上工作,这是可行的,否则你可以尝试@Grzegorz-Skibinski 所说的通用方法
xor操作将在较新版本中弃用。Pandas建议使用symmetric_difference。
cols_to_excl = ['Class']
df.loc[df[df.columns.symmetric_difference(cols_to_excl)].isnull().any(axis=1)]