简体   繁体   中英

How to replace only first Nan value in Pandas DataFrame?

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)

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)))

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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM