2

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?

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

That worked for the first entry where a condition was not met, however, it didn't reapply the condition when I tried to use it for the entire dataframe.
@ZivLotzky Sorry about that... try it now?
Not exactly... I want to return True if a > 0 and b > 0 and False if a < 0 and b < 0. However, your code is returning True when either condition is met and False when neither is met. Instead, if a condition is not met, I wish to return the previous result. For example, if row 1 returns False, but no condition is met for row 2, it should also return False.
As a 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? This might be a different answer since you do not have a uniform condition of greater than or equal to 0? @coldspeed
@ZivLotzky Yeah, I think the answer will be different... would you like to open another question?
|
1
i = np.sign(df.a)
j = np.sign(df.b)

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

1 Comment

Yes, this is what I came up with in the end... sorry for the retries, brain's a bit slow this monday... refresh your page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.