How to perform a single operation on multiple columns of a Dataframe

I have the following framework:

df >>> TSLA MSFT 2017-05-15 00:00:00+00:00 320 68 2017-05-16 00:00:00+00:00 319 69 2017-05-17 00:00:00+00:00 314 61 2017-05-18 00:00:00+00:00 313 66 2017-05-19 00:00:00+00:00 316 62 2017-05-22 00:00:00+00:00 314 65 2017-05-23 00:00:00+00:00 310 63 max_idx = df.idxmax() # returns index of max value >>> TSLA 2017-05-15 00:00:00+00:00 >>> MSFT 2017-05-16 00:00:00+00:00 max_value = df.max() # returns max value >>> TSLA = 320 >>> MSFT = 69 def pct_change(first, second): # pct chg formula return (second-first) / first*100.00 

I want to get the percentage change between max_value and with each consecutive value, starting with max_idx ( df.loc[max_idx:] ) for both columns. Just to make sure the percentage change is not lower than 5%.

 Example: for TSLA: 320 with 319 = 2% for MSFT: 69 with 61 = 4% 320 with 314 = 4% 69 with 66 = 5% 320 with 313 = 5% 69 with 62 = 10% 

Edit: If it’s hard for you to answer, I can only be satisfied with a reference to what type of function or method I will use for such operations.

Note. I just want the percentage change not to be lower than 5%.

0
source share
1 answer

I'm not sure about your true / false conditions, but I think you need something similar, thanks @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,

Formula John:

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

Return:

 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 these values ​​are greater than 5, return True, else return False.

My original formula also works, just a little more calculation to do the same thing as John's formula. Finally, use the lambda function to apply this formula to each column independently.

+2
source

Source: https://habr.com/ru/post/1270305/


All Articles