pandas :如果值小于以前的值,如何检查多列? [英] Pandas: How can I check multiple columns if there are any values that are smaller than previous value?
本文介绍了 pandas :如果值小于以前的值,如何检查多列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Solution for a single column is already provided here: Pandas: Check if column value is smaller than any previous column value.
但是,我的数据集由许多列组成,我不想暴力破解许多代码.
However, my dataset consists of many columns and I don't want to brute force many codes.
示例数据集:
c d e
0 3 5 8
1 1 5 8
2 5 6 8
3 6 7 8
4 2 1 9
5 9 3 3
所需结果:
c d e c_diff d_diff e_diff
0 3 5 8 False False False
1 1 5 8 True False False
2 5 6 8 False False False
3 6 7 8 False False False
4 2 1 9 True True False
5 9 3 3 False False True
是否可以通过简单的Python/Panda代码行来执行此任务?
Is there any way to perform this task with simple lines of Python/Panda code?
推荐答案
我们可以使用 DataFrame.lt
We can use DataFrame.diff
with DataFrame.lt
df.diff().lt(0).add_suffix('_diff')
c_diff d_diff e_diff
0 False False False
1 True False False
2 False False False
3 False False False
4 True True False
5 False False True
这篇关于 pandas :如果值小于以前的值,如何检查多列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文