python - 使用跨多列的列表理解将所有非 NaN 值转换为 1
问题描述
这里有一些数据:
test = pd.DataFrame([[np.nan,"cat","mouse", 'tiger'],
["tiger","dog","elephant", "mouse"],
["cat",np.nan,"giraffe", "cat"],
[np.nan,np.nan,"ant", "ant"]], columns=["animal1","animal2","animal3", "animal4"])
我想将所有 NaN 转换为 0,并将所有响应转换为 1。
#First I convert all NaNs to 0 and make everything string
test = test.fillna(0)
test = test.astype(str)
然后我创建一个感兴趣的列的列表(这在这个例子中没有意义,因为只有 2 列,但在我的实际情况下有很多)
op = test.iloc[:,0:2].columns.tolist()
我本来以为我可以这样做:
test[op] = [1 if x != '0' else 0 for x in test[op]]
但它不起作用,因为它将所有内容都转换为 1。
然后我尝试手动执行每一列,它确实有效:
test['animal1'] = [1 if x != '0' else 0 for x in test['animal1']]
任何人都知道为什么后一种方式有效但不是前者?任何有关如何使其工作的指导将不胜感激。
编辑/更新:SeaBean 提供了一个可行的解决方案(谢谢!!)。我仍然很想知道为什么我使用的方法只在一次做一列(手动)时才有效。
解决方案
您可以使用 .isna() 并反转结果:
print(~test.isna())
animal1 animal2 animal3 animal4
0 False True True True
1 True True True True
2 True False True True
3 False False True True
如果你宁愿有 0 和 1 乘以 1:
print((~test.isna())*1)
animal1 animal2 animal3 animal4
0 0 1 1 1
1 1 1 1 1
2 1 0 1 1
3 0 0 1 1
推荐阅读
- ckeditor - CKEditor gui 显示为灰色/禁用
- vue.js - Vue.js vuetify v-textarea 在未展开的 v-layout 中无法正确自动增长
- android - 我怎样才能为每天的其他工作(如提醒)创建任何警报?
- gitlab - 仓库中的 Jenkinsfile 与代码不同
- javascript - 如何在点击事件或上下文之外访问变量
- android - 如何找到运行 64 位操作系统的 Android 手机进行软件测试?
- excel - 运行时错误 424,带有基于选择的隐藏框
- javascript - 搜索关键字数组以查看是否有任何字符与用户输入匹配
- ubuntu - 安装 Windows 10 时可启动 USB 不工作
- php - 我的域名后面是 index.php ,当我打开任何网页时