python - 比较多个行值

标签 python python-3.x pandas dataframe

我想比较每一行的 A 值与其他列

这个问题比较复杂,但我试图在这张表中简化它:

     A    B    C  D
0  1.3  1.0  1.1  1
1  2.5  2.9  2.6  3
2  3.1  3.0  3.2  2

结果应该是这样的:

这里索引0中:1.3大于B,C,D中的值,则返回1,否则为0

     A    B    C  D  result
0  1.3  1.0  1.1  1       1
1  2.5  2.9  2.6  3       0
2  3.1  3.0  3.2  2       0

提前致谢

最佳答案

使用assign创建新列
使用 df.le(df.A, 0) 将列 'A' 与所有其他列进行比较
使用 all(1) 查找所有列的 True
使用astype(int)使其成为10

df.assign(result=df.lt(df.A, 0).all(1).astype(int))

     A    B    C  D  result
0  1.3  1.0  1.1  1       1
1  2.5  2.9  2.6  3       0
2  3.1  3.0  3.2  2       0

关于python - 比较多个行值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42643573/

相关文章:

python - 在Python中获取两个网络元素之间的距离?

python - 在 MPI Python 程序中查找 SegFault 的一般建议

python - 模块 'scipy.special' 在 Python/SciPy 中没有 'expit' 成员

python - 从 n 个相等的项目中挑选与最小数字相关的项目

python - 在 GCP 上使用 python numpy 和 pandas 部署小批量作业

python - 我可以访问 shopify 中的商店权限吗

python - 修正伪代码编写的双重求和公式

android - 改造未正确接收响应

python - 将 Pandas 数据框转换为字典

python - Pandas : Delete rows based on other rows