Skip to content Skip to sidebar Skip to footer

How To Perform Single Operation On Multiple Columns Of Dataframe

I have the following dataframe: df >>> TSLA MSFT 2017-05-15 00:00:00+00:00 320 68 2017-05-16 0

Solution 1:

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:000.0000002017-05-16 00:00:00+00:000.0031252017-05-17 00:00:00+00:000.0187502017-05-18 00:00:00+00:000.0218752017-05-19 00:00:00+00:000.0125002017-05-22 00:00:00+00:000.0187502017-05-23 00:00:00+00:000.031250Name: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.

Post a Comment for "How To Perform Single Operation On Multiple Columns Of Dataframe"