检查数据框中仅存在列值 [英] Checking for only column value to be present in dataframe
本文介绍了检查数据框中仅存在列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在下面的数据框中有以下内容:
I have a following below dataframe :
col1 col2 col3
0 1601286 NAN NAN
1 1601286 1135 2018-12-31
2 1601286 NAN NAN
3 1601286 1135 2018-12-31
4 1601286 NAN 2018-12-31
5 1601286 1135 2018-12-31
6 1601286 1135 2018-12-31
7 1601286 1135 2018-12-31
8 1601286 NAN NAN
我需要验证一下这三列中只有一列应该有一个值.如果notnull()不止一个,则应为False
.
I need to put a validation that only one out of these 3 columns should have a value . If more than one is notnull(), it should be a False
.
例如,上述数据的输出应为,
For example, the output of above data should be ,
0 True
1 False
2 True
3 False
4 False
5 False
6 False
7 False
8 True
尝试执行以下肯定可以做的事情:-
trying to do something like below which is for sure will not work:-
df= df[['col1', 'col2', 'col3']].notnull().any(axis=1)
我该如何处理.
推荐答案
使用 sum
:
df.isnull().sum(1).eq(2)
或:
df.isnull().sum(1).gt(1)
或:
df.notnull().sum(1).lt(2)
或:
df.notnull().sum(1).eq(1)
0 True
1 False
2 True
3 False
4 False
5 False
6 False
7 False
8 True
dtype: bool
这篇关于检查数据框中仅存在列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文