遍历数据框的行并检查重复项

时间:2018-08-04 23:51:08

标签: python pandas dataframe

signal = pd.DataFrame(
        [
            [0, 0, 0],
            [-1, -1, -1],
            [1, 0, -1],
            [0, 0, 0],
            [1, 0, 0],
            [0, 1, 0],
            [0, 0, 1],
            [0, -1, 1],
            [-1, 0, 0],
            [0, 0, 0]],columns=['A','B','C'])

信号:

     A   B   C
0    0   0   0
1   -1  -1  -1
2    1   0  -1
3    0   0   0
4    1   0   0
5    0   1   0
6    0   0   1
7    0  -1   1
8   -1   0   0
9    0   0   0

我的目标是一次遍历信号数据帧的三个索引,并检查是否存在重复项(如果存在),使后者等于零

例如在信号数据帧中,如果索引位于A [2],我将检查值A [2],A [3]和A [4]。请注意,A [2]和A [4]的值均为1,我会将A [4]更改为0

下面的滤波信号数据帧是我期望的结果

滤波信号

    A   B   C
0   0   0   0
1  -1  -1  -1
2   1   0   0
3   0   0   0
4   0   0   0
5   0   1   0
6   0   0   1
7   0  -1   0
8  -1   0   0
9   0   0   0

1 个答案:

答案 0 :(得分:0)

如果我们创建第一列的列表:

l = df['A']
#we copy the list just to compare it later for confirmation.
m = l.copy()
#iterate through the list and assign 0 if duplicates.
for i in range(0, len(m), 3):
    if m[i + 1] == m[i]:
        m[i+ 1] = 0
    if m[i + 2] == m[i]:
        m[i + 2] = 0
#re-assign the list to the column
df['A'] = m