Skip to content
Advertisement

skip na while removing consecutive same numbers in a column in python

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

A   B    C
12  14  16
15  16  17
15  16  18
15  18  20

after the first loop i have 
A    B    C
12   14  16
15   16  17
na   na  18
15   18  20

now i have to remove the last 15 also as i want the dataframe to be 
A    B   C
12   14  16
15   16  17
na   na  18
na   18  20

the code is

for i in df1.index:
   f_id = df1["f_secid"][i]
   b_id = df1["b_secid"][i]
   for j in range(len(f_data)-1)
       if j ==0
          continue
       elif (f_data[f_id][j] == math.nan) & (f_data["f_id"][j+1] == f_data["f_id"][j-1])
             f_data[f_id][j+1] = math.nan 

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:

dfo = df.mask(df.diff().eq(0))
print(dfo)

Result

      A     B   C
0  12.0  14.0  16
1  15.0  16.0  17
2   NaN   NaN  18
3   NaN  18.0  20
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement