I am trying to create a column with values from one column, but based on matching another column with the previous value.
Here is my current code:
d = {'a':[1,2,3,1,2,3,2,1], 'b':[10,20,30,40,50,60,70,80]}
df = pd.DataFrame(d)
df['c'] = df['b'][df['a'] == df['a'].prev()]
And my desired result:
a b c
0 1 10 NaN
1 2 20 NaN
2 3 30 NaN
3 1 40 10
4 2 50 20
5 3 60 30
6 2 70 50
7 1 80 40
... which I do not receive, because .prev()- this is not the real thing. Any thoughts?
source
share