从数据框中获取上一个和下一个值,并添加一个新列

我是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]

这样,这样。即使是给定的数据,我们也可以使用。

bei520 回答:从数据框中获取上一个和下一个值,并添加一个新列

这应该为您提供正确的方向:

import pandas as pd
# create a dummy df
df = pd.DataFrame()
df['feature'] = range(100)
df = df.sample(frac=1)
# create shifted columns
df['shift1'] = df['feature'].shift()
df['shift2'] = df['feature'].shift(2)
# concat the previous values
values = df.loc[:,['shift1','shift2']].values
df['prev'] = values.tolist()
# you just want the zeros,right?
df.query('feature == 0')
,

您可以使用列表推导:

x = df['feature'].tolist()
y = x[::-1]
df['previous'] = [y[-i:][:3] for i in range(1,len(x)+1)]
df['Next'] = [x[i: i + 3] for i in range(1,len(x) + 1)]

df['previous'] = df['previous'].shift(1).where(df['feature'] == 0,'-')
df['Next'] = df['Next'].where(df['feature'] == 0,'-')
print (df)
    Offset  feature      previous          Next
0        0        2             -             -
1        5        2             -             -
2       11        0        [2,2]   [22,22,0]
3       21       22             -             -
4       28       22             -             -
5       32        0   [22,0]  [21,21,21]
6       38       21             -             -
7       42       21             -             -
8       52       21             -             -
9       55        0  [21,21]     [0,1,1]
10      58        0   [0,21]     [1,1]
11      62        1             -             -
12      66        1             -             -
13      70        1             -             -
14      73        0     [1,1]        [1,1]
15      78        1             -             -
16      79        1             -             -
本文链接:https://www.f2er.com/3168657.html

大家都在问