I have a pandas Dataframe, where I have to compare values of two adjacent rows of a particular column and if they are equal then in a new column 0 needs to be added in the corresponding first row or 1 if the value in the second row is greater than the first or -1 if it's smaller. For example, such an operation on the following Dataframe dataframe before the operation
column1
0 2
1 2
2 4
3 4
4 5
5 3
6 2
7 1
8 55
9 3
should give the following output
dataframe after the operation
column1 column2
0 2 0
1 2 1
2 4 0
3 4 1
4 5 -1
5 3 -1
6 2 -1
7 1 1
8 55 -1
9 3 0
What we are looking for is the sign of the change. We break this up into 3 steps:
diff
will take the differences of each row with the prior row This captures the change.x / abs(x)
is common way to capture the sign of something. We use it here when we divide d
by d.abs()
.nan
in the first position due to diff
and when we divide by zero. We can fill them in with zero.df = pd.DataFrame(dict(column1=[2, 2, 4, 4, 5, 3, 2, 1, 55, 3]))
d = df.column1.diff()
d.div(d.abs()).fillna(0)
0 0.0
1 0.0
2 1.0
3 0.0
4 1.0
5 -1.0
6 -1.0
7 -1.0
8 1.0
9 -1.0
Name: column1, dtype: float64
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With