Skip to content
Advertisement

python find the nearest nonzero element in df column

I have df:

id   number
1     5
1      0
1      0
1      2    
2      0
3      1

I want to write a function to fill 0 values.I want for each id(for each group) , when the value in number column is zero, to search the closet non zero value in the column and return the value. for example to id 1 to fill the second and third-row with 2. If I dont have such value like in id 2 , just to remain it as is. How can I do that?

Advertisement

Answer

You can mask the 0, bfill per group, finally fillna with then original value for the groups than only have zeros:

df['number2'] = (df['number']
 .mask(df['number'].eq(0))
 .groupby(df['id'])
 .bfill()
 .fillna(df['number'], downcast='infer')
)

output:

   id  number  number2
0   1       5        5
1   1       0        2
2   1       0        2
3   1       2        2
4   2       0        0
5   3       1        1
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement