如果多个列具有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:值的长度与索引的长度不匹配
答案 0 :(得分:2)
此处是按嵌套列表过滤的必要条件:
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)