Python 基于多个条件追加列

Python 基于多个条件追加列,python,pandas,numpy,Python,Pandas,Numpy,我想创建一个新列,如果多个列具有nan值,则添加数字1。但是,当我运行我编写的代码时,我不断遇到错误消息 df_test2['notsure']=np.where((df_test2[df_test2[['android','blackberry','chrome_os','linux', 'macintosh','tizen','windows_phone','windows', 'ipad','iphone','devi

我想创建一个新列,如果多个列具有nan值,则添加数字1。但是,当我运行我编写的代码时,我不断遇到错误消息

df_test2['notsure']=np.where((df_test2[df_test2[['android','blackberry','chrome_os','linux',
                  'macintosh','tizen','windows_phone','windows',
                  'ipad','iphone','device_other']].isna().any(1)]),1,0)
错误消息:

ValueError:值的长度与索引的长度不匹配


以下是必要的按嵌套列表筛选:

cols = ['android','blackberry','chrome_os','linux',
        'macintosh','tizen','windows_phone','windows',
        'ipad','iphone','device_other']

df_test2['notsure'] = np.where(df_test2[cols].isna().any(1),1,0)
另一种方法是将布尔掩码转换为整数,以便将
True/False
映射为
1,0
映射:

df_test2['notsure'] = df_test2[cols].isna().any(1).astype(int)