pandas - 熊猫识别是否有任何元素在一行中
问题描述
我有一个数据框,它是单行数值,我想知道这些值中的任何一个是否大于 2,如果是,则创建一个带有单词“Diff”的新列
Col_,F_1,F_2
1,5,0
我的数据框是 diff_df。这是我尝试过的一件事
c = diff_df >2
if c.any():
diff_df['difference']='Difference'
如果我要打印 c。这将是
Col_,F_1,F_2
False,True,False
我已经尝试过 c.all() 和许多其他东西的迭代。显然,我的经验不足让我退缩了,谷歌在这方面没有帮助。我尝试的一切都是“系列(或数据框)的真值使用 a.any()、a.all().... 不明确。”任何帮助将不胜感激。
解决方案
Since it is only one row, take the .max().max()
of the dataframe. With one .max()
you are going to get the .max()
of each column. The second .max()
takes the max of all the columns.
if diff_df.max().max() > 2: diff_df['difference']='Difference'
output:
Col_ F_1 F_2 difference
0 1 5 0 Difference
推荐阅读
- discord.js - 为什么这个机器人在“user.hasPermission()”上失败了?
- python - 在管理页面上添加表时 Django 出错
- excel - 合并来自已关闭工作簿的数据 adodb 方法
- unity3d - Sing app bundle with lost keystore 如何更新?
- scala - 如何在 Scala 中分解我自己的序列 ADT?
- python - RepeatVector 层与 Concatenate 层相同吗?
- c++ - 将对象的值保存在向量中,并将指针保存在地图中。这个对吗?
- javascript - 为什么向按钮添加类仅在第二次单击后才有效?
- video - ffmpeg 出错,错误为设置`-profile:v main` 初始化输出流 0:0
- javascript - 无法在jQuery中(两者)之前和之后插入元素