我是python和pandas的新手。在这里,我有一个数据框,就像
Id Offset feature
0 0 2
0 5 2
0 11 0
0 21 22
0 28 22
1 32 0
1 38 21
1 42 21
1 52 21
1 55 0
1 58 0
1 62 1
1 66 1
1 70 1
2 73 0
2 78 1
2 79 1
现在在此df中,我有一个功能栏。我正在尝试对本专栏做一些操作。此列具有一些值。它还具有一个0
值。现在,我想基于此0的前三个值替换该值。
如果我们看到,具有先前值的前0个是[2,2],因为它是第一个,因此它不会得到第三个,而后三个是[22,22,0]。
现在我正在尝试获取以下数据框
预期产量
Offset feature previous Next NewFeature
0 2 - - 2
5 2 - - 2
11 0 [2,2] [22,0] 0
21 22 - - 22
28 22 - - 22
32 0 [22,0] [21,21,21] 0
38 21 - - 21
42 21 - - 21
52 21 - - 21
55 0 [21,21] [0,1,1] 0
58 0 [0,21] [1,1] 0
62 1 - - 1
66 1 - - 1
70 1 - - 1
73 0 [1,1] [1,1] 1
78 1 - - 1
79 1 - - 1
因此,在此,我试图检查上一个和下一个是否相同。
有什么办法可以获取此数据框?如何获得此数据框中的上一个和下一个值?任何帮助都会很棒。 谢谢
因此,获取newFeature
的逻辑是。
这里有功能列表,
1,2,16,15,26,25
if the previous and next array has values like,(1,15) then it is the same as 1. and if it is from (2,25) then we can replace it with the 2.
如果
previous values are [1,2] and next are [1,1] then in this as I said earlier (1,15) are 1 only .. so the number of 1 are more than 2 so,the 0 will get replaced by 1. and 26 will become 2
就像它将变成[1,2]和[1,1]
这样,这样。即使是给定的数据,我们也可以使用。