How to perform single operation on Multiple columns of Dataframe

后端 未结 1 1336
谎友^
谎友^ 2021-01-17 00:19

I have the following dataframe:

df
>>>                                     TSLA             MSFT
2017-05-15 00:00:00+00:00                320               


        
相关标签:
1条回答
  • 2021-01-17 00:59

    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())
    

    Or using your logic:

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

    Output:

    TSLA    False
    MSFT    False
    dtype: bool
    

    Let's look at one column,

    John's formula:

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

    Returns:

    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
    

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

    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.

    0 讨论(0)
提交回复
热议问题