I have a DataFrame with 2 columns, a
and b
, and I would like to populate a third column, c
based on the following three conditions:
- if
a.diff() > 0
thenc = b.shift() + b
- elif
a.diff() < 0
thenc = b.shift() - b
- elif
a.diff() == 0
thenc = b.shift()
What is a Pythonic, one-liner way of doing this?
Example:
a b c
0 2 10 Nan
1 3 16 26
2 1 12 4
3 1 18 12
4 3 11 29
5 1 13 -2