Updated:
In this post, the first answer is very close to solve this problem too. However it does not take into account the column A and C.
Pandas Average If in Python : Combining groupby mean with conditional statement
There is a DataFrame with 3 columns. I would like to add 2 new columns which are:
- the rolling avg of B by A and C (rolling 2 of the current and the previous row which are pass the statement - the same A and C)
- the rolling avg of B by A and C (rolling 2 of the previous 2 which are pass the statement - the same A and C)
For the second part, I have date and a sequence which could be used as the basic of rolling avg calculation.
Any ideas?
df = pd.DataFrame({'A': ['t1', 't1', 't1', 't1', 't2', 't2', 't2', 't2','t1'],
'B': [100, 104, 108, 110, 102, 110, 98, 100, 200],
'C': ['h', 'a', 'a', 'a', 'a', 'h', 'h', 'h','h'],
'expected1': [100, 104, 106, 109, 102, 110, 104, 99, 150],
'expected2': [0, 0, 104, 106, 0, 0, 110, 104, 100]}, columns=['A', 'B', 'C','expected1','expected2'])
df