Negation in np.select() condition

别等时光非礼了梦想. 提交于 2020-02-23 07:13:43

问题


Here is my code:

import pandas as pd
import numpy as np

df = pd.DataFrame({ 'var1': ['a', 'b', 'c',np.nan, np.nan],
                   'var2': [1, 2, np.nan , 4, np.nan]
                 })



conditions = [
    (not(pd.isna(df["var1"]))) & (not(pd.isna(df["var2"]))),
    (pd.isna(df["var1"])) & (pd.isna(df["var2"]))]

choices = ["No missing", "Both missing"]

df['Result'] = np.select(conditions, choices, default=np.nan)

Output:

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1478, in __nonzero__
    f"The truth value of a {type(self).__name__} is ambiguous. "

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Problem is with line (not(pd.isna(df["var1"]))) & (not(pd.isna(df["var2"]))). This line should give TRUE when in both var1 and var2 in not a NaN value. Problem here is with negation, because with conditions without negation there is no problem.

Question: How can to correct (not(pd.isna(df["var1"]))) & (not(pd.isna(df["var2"]))) line so in case when in both var1 and var2 in not a NaN value the condition should give TRUE?


回答1:


Try:

conditions = [(~pd.isna(df["var1"]) & ~pd.isna(df["var2"])),
               (pd.isna(df["var1"]) &  pd.isna(df["var2"]))]


来源:https://stackoverflow.com/questions/60099141/negation-in-np-select-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!