Home > front end >  Replace only first occurrence of blank value
Replace only first occurrence of blank value

Time:07-14

I am trying to find first blank values in a dataframe and replace ONLY first blank value in row with 9999, leaving others blanks untouched.

enter image description here

expected output:

enter image description here

I have tried using few tricks but unable to work my way dfdf.isnull() & (df.isnull().cumsum()=1)]='9999'

Any help would be appreciated.

CodePudding user response:

You can use fillna():

df.fillna(9999,limit=1,axis=1)

  node oam   mmu   rau  minq minx
0  ABC   1  mmu1  9999   NaN  NaN
1  DEF   2  mmu2    x2  9999  NaN
2  GKH   3  mmu3    x3  9999  NaN

CodePudding user response:

In your case do

df = df.mask(df.isnull().cumsum(1).eq(1) & df.isnull(),99999)
Out[315]: 
  node  oam  mmu    rau       mm  bb
0  abc    1    1  99999      NaN NaN
1  def    2    2     x2  99999.0 NaN
2  ghk    3    3     x3  99999.0 NaN
  • Related