[英]If statement in pandas DataFrame, boolean column calculated from three other columns that may contain nan
I am determining a boolean to put in column A
, given the values of column B
, C
, and D
. 给定B
, C
和D
列的值,我正在确定要放入A
列的布尔值。
Columns B
, C
, D
contain booleans, but also possibly nan
. 列B
, C
, D
包含布尔值,但也可能包含nan
。
I need this statement to result in the following 我需要此语句才能导致以下结果
Column A
should be... A
栏应为...
True if [B,C,D] = [False, False, False]
False if [B,C,D] contains True, regardless of other values
nan if columns [B,C,D] contain only False or nan
Unfortunately, nan
values are returning False
, so the following code does not work. 不幸的是, nan
值返回False
,因此以下代码不起作用。
df['A'] = ~(df['B'] | df['C'] | df['D'])
Thank you. 谢谢。
Edit to clarify wording: 编辑以澄清措辞:
The above three situations were considered to follow one another. 以上三种情况被认为是相互遵循的。 The only condition which returns True is FFF. 返回True的唯一条件是FFF。 Any true in the set should return false. 集合中的任何true应该返回false。 And any set that contains some combo of FF and nan should return nan. 并且任何包含FF和nan组合的集合都应返回nan。 In the third situation, I can no longer say for sure that this row is triple negative, and so I return nan. 在第三种情况下,我不能再确定该行是否为三重负数,因此我返回nan。 Any row with a True in B|C|D is definitely not triple negative. B | C | D中具有True的任何行绝对不是三重负数。
As pointed out in the comments, there is an ambiguity in the wording, as an all- False
row could have two possible return values. 正如评论中指出的那样,在措辞上存在歧义,因为全False
行可能有两个可能的返回值。 Assuming that the all- False
case should give a True
, the following should do what you are looking for: 假设全为False
情况应为True
,则应执行以下操作:
In [41]: df
Out[41]:
B C D
0 False False False
1 True False NaN
2 NaN False False
In [42]: df['A'] = df.apply(lambda row: True if not any(row) else False if True in list(row) else np.nan, axis=1)
In [43]: df
Out[43]:
B C D A
0 False False False True
1 True False NaN False
2 NaN False False NaN
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.