Home > Enterprise >  How to replace only first Nan value in Pandas DataFrame?
How to replace only first Nan value in Pandas DataFrame?

Time:07-26

I am trying to replace Nan with a list of numbers generated by a random seed. This means each Nan value needs to be replaced by a unique integer. Items in the columns are unique, but the rows just seem to be replicating themselves? Any suggestions would be welcome

np.random.seed(56)
rs=np.random.randint(1,100, size=total)

df=pd.DataFrame(index=np.arange(rows), columns=np.arange(columns))
    
for i in rs:
    df=df.fillna(value=i, limit=1)

CodePudding user response:

the df.fillna() will replace all the values that contains NA. So your code is actually changing the NA values just at the first iteration of the forloop because than, no other values to fill remains.

You can use the applymap function to iterates through all the rows and fill the NANs with a randomly generated values in this way:

df.applymap(lambda l: l if not np.isnan(l) else np.random.randint(1,100, size=total)))

CodePudding user response:

You can try stack

s = df.stack(dropna=False)
s = s[s.isna()]
s[:] = rs
df.update(s.unstack())

Or we can create from original

df = pd.DataFrame(data = rs.reshape(row, col),
                  index=np.arange(row), 
                  columns=np.arange(col))
  • Related