I have to remove the consecutive same number from the column in a dataframe. I am able to remove the number one time, but when I try to do it for the second time the loop does not work as there is one na value in between.
dataframe is
JavaScript
x
32
32
1
A B C
2
12 14 16
3
15 16 17
4
15 16 18
5
15 18 20
6
7
after the first loop i have
8
A B C
9
12 14 16
10
15 16 17
11
na na 18
12
15 18 20
13
14
now i have to remove the last 15 also as i want the dataframe to be
15
A B C
16
12 14 16
17
15 16 17
18
na na 18
19
na 18 20
20
21
the code is
22
23
for i in df1.index:
24
f_id = df1["f_secid"][i]
25
b_id = df1["b_secid"][i]
26
for j in range(len(f_data)-1)
27
if j ==0
28
continue
29
elif (f_data[f_id][j] == math.nan) & (f_data["f_id"][j+1] == f_data["f_id"][j-1])
30
f_data[f_id][j+1] = math.nan
31
32
I dont know why the last line of code i.e. elif condition is not working.
Advertisement
Answer
If you were willing to abandon the loop idea you could:
JavaScript
1
3
1
dfo = df.mask(df.diff().eq(0))
2
print(dfo)
3
Result
JavaScript
1
6
1
A B C
2
0 12.0 14.0 16
3
1 15.0 16.0 17
4
2 NaN NaN 18
5
3 NaN 18.0 20
6