Ziv Lotzky
Ziv Lotzky

Reputation: 45

Rolling Conditional Pandas DataFrame Column

How would I be able to write a rolling condition apply to a column in pandas?

import pandas as pd
import numpy as np    

lst = np.random.random_integers(low = -10, high = 10, size = 10)
lst2 = np.random.random_integers(low = -10, high = 10, size = 10)

#lst = [ -2  10 -10  -6   4   2  -5   4   9   3]
#lst2 = [-7  5  6 -4  7  1 -4 -6 -1 -4]
df = pandas.DataFrame({'a' : lst, 'b' : lst2})

Given a dataframe, namely 'df', I want to create a column 'C' such that it will display True if the element in a > 0 and b > 0 or False if a < 0 and b < 0.

For the rows that do not meet this condition, I want to roll the entry in the previous row to the current one (i.e. if the previous row has value 'True' but does not meet the specified conditions, it should have value 'True'.)

How can I do this?

Follow-up Question : How would I do this for conditions a > 1 and b > 1 returns True or a < -1 and b < -1 returns False?

Upvotes: 2

Views: 620

Answers (2)

Ziv Lotzky
Ziv Lotzky

Reputation: 45

i = np.sign(df.a)
j = np.sign(df.b)

i.mask(i != j).ffill().ge(0)

Upvotes: 1

cs95
cs95

Reputation: 402263

I prefer to do this with a little mathemagic on the signs.

i = np.sign(df.a)
j = np.sign(df.b)

i = i.mask(i != j).ffill()
i >= 0

# for your `lst` and `lst2` input 
0    False
1     True
2     True
3    False
4     True
5     True
6    False
7    False
8    False
9    False
Name: a, dtype: bool

As long as you don't have to worry about integer overflows, this works just fine.

Upvotes: 2

Related Questions