且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何对数据框的多列执行单个操作

更新时间:2023-11-18 23:22:40

我不确定您的正确/错误条件,但由于@JohnGalt,我想您需要类似的东西:

I am not sure about your true/false conditions, but I think you need something like this, thanks to @JohnGalt:

df.apply(lambda x: ((1 - x/x.max()) > 0.05).all())

或使用您的逻辑:

df.apply(lambda x: ((x[x.idxmax()]-x)/x[x.idxmax()]*100>5).all())

输出:

TSLA    False
MSFT    False
dtype: bool

我们来看一列,

约翰的公式:

1 - df.TSLA/df.TSLA.max()

返回:

2017-05-15 00:00:00+00:00    0.000000
2017-05-16 00:00:00+00:00    0.003125
2017-05-17 00:00:00+00:00    0.018750
2017-05-18 00:00:00+00:00    0.021875
2017-05-19 00:00:00+00:00    0.012500
2017-05-22 00:00:00+00:00    0.018750
2017-05-23 00:00:00+00:00    0.031250
Name: TSLA, dtype: float64

如果所有这些值均大于5,则返回True,否则返回False.

If all of those values are greater than 5 return True, else return False.

我的原始公式也可以工作,只需要更多的计算即可完成与John公式相同的操作. 最后,使用lambda函数将此公式独立应用于每个列.

My original formula works also, just a bit more calculation to do the same thing that John formula does. Lastly, use lambda function to apply this formula to each column independently.