How to create a lambda function to return next cell value?
Question:
For example :
Val1
Val2
48
21
156
45
61
54
12
79
64
45
the excepted output is the followwing :
Val1
Val2
Output
48
21
45
156
45
54
61
54
79
12
79
45
64
45
What I did try is the following but it does not work and I am not sure if it is possible to do with lambda
.
)
)
DF["Output"] = DF.apply(lambda x: x[1].val2, axis=1)
The final goal is to apply thanks to a lambda function the following calculation :
(21/45)
(45/54)
(54/79)
(79/45)
…
Wihtout having to create a new column with the shifted values as suggested in Ynjxsjmh Answer
Answers:
Using Series.shift(-1)
DF["Output"] = DF["Val2"].shift(-1)
For example :
Val1 | Val2 |
---|---|
48 | 21 |
156 | 45 |
61 | 54 |
12 | 79 |
64 | 45 |
the excepted output is the followwing :
Val1 | Val2 | Output |
---|---|---|
48 | 21 | 45 |
156 | 45 | 54 |
61 | 54 | 79 |
12 | 79 | 45 |
64 | 45 |
What I did try is the following but it does not work and I am not sure if it is possible to do with lambda
.
)
)
DF["Output"] = DF.apply(lambda x: x[1].val2, axis=1)
The final goal is to apply thanks to a lambda function the following calculation :
(21/45)
(45/54)
(54/79)
(79/45)
…
Wihtout having to create a new column with the shifted values as suggested in Ynjxsjmh Answer
Using Series.shift(-1)
DF["Output"] = DF["Val2"].shift(-1)