I have Dataframe as given below

import pandas as pd
import numpy as np

k={'ID':[1,2,3,4,5,6],'m1':[20,'',30,40,50,60],
   'm2':['',40,40,90,'',''],
   'm3':['','','','','',''],
   'm4':['','','','','',''],
   'm5':['','','','',1,''],
   'm6':[10,'','',90,'','']}

df=pd.DataFrame(data=k)

we have check if there any exiting value in other of same row then we need to if fill it with zero or others does contain any value we have to fill it, NAN

my result show look this

ID  m1  m2  m3  m4  m5  m6
1   20  0.0 0.0 0.0 0.0 10.0 # first row and last row has value some vale so  we have fill it Zero others rows  
2   0   40.0    NaN NaN NaN NaN  # there are no value after the second row we have will it with NAN
3   30  40.0    NaN NaN NaN NaN  # there are no value after the second row we have will it with NAN
4   40  90.0    0.0 0.0 0.0 90.0 # first row,second row  and last row has some value so we have fill it Zero to other rows  
5   50  0.0 0.0 0.0 1.0 NaN   # first row,and fifth row has some value so we have fill it Zero to other rows and last row with Nan  
6   60  NaN NaN NaN NaN NaN # there are no value after the first row we have will it with NAN

1

There are 1 answers

0
BENY On BEST ANSWER

Let us try bfill with mask

s=df.iloc[:,1:]
df.iloc[:,1:]=s.mask(s.mask(s=='').bfill(1).notna()&(s==''),0)
df
   ID  m1  m2 m3 m4 m5  m6
0   1  20   0  0  0  0  10
1   2   0  40             
2   3  30  40             
3   4  40  90  0  0  0  90
4   5  50   0  0  0  1    
5   6  60