BrkPressState | VehSpdGS |
---|---|
1 | 2 |
1 | 3 |
1 | 2 |
1 | 4 |
0 | 12 |
0 | 13 |
0 | 11 |
1 | 3 |
0 | 15 |
0 | 14 |
0 | 15 |
1 | 12 |
1 | 13 |
0 | 14 |
For the above table i am trying to populate the next row value in previous last event, Like the below table
I tried with Shift – 1 but its populating only for the current row , Sample code which i tried.
JavaScript
x
2
1
d['result']=d.loc[d['BrkPressState'] != d['BrkPressState'].shift(-1), 'VehSpdGS']
2
Expected output:
Advertisement
Answer
Let us do diff
to compare the previous and current row in BrkPressState
column in order to identify boundaries, then mask
and shift
the values in VehSpdGS
column
JavaScript
1
3
1
m = df['BrkPressState'].diff().ne(0)
2
df['Results'] = df['VehSpdGS'].mask(~m).shift(-1)
3
JavaScript
1
16
16
1
BrkPressState VehSpdGS Results
2
0 1 2 NaN
3
1 1 3 NaN
4
2 1 2 NaN
5
3 1 4 12.0
6
4 0 12 NaN
7
5 0 13 NaN
8
6 0 11 3.0
9
7 1 3 15.0
10
8 0 15 NaN
11
9 0 14 NaN
12
10 0 15 12.0
13
11 1 12 NaN
14
12 1 13 14.0
15
13 0 14 NaN
16