Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
561 B
2
Indexable
Never
import numpy as np

# get dimensions of df
nrows, ncols = len(df.index), 30          

volume = nrows * ncols                    # total number of entries in df
volume_to_be_nan = int(volume * 0.1)      # number of entries to turn to NaN (10 %)

# randomly generate index locations for the new NaNs
indices = np.random.randint(volume, size=volume_to_be_nan)
row_indices = indices % nrows
col_indices = (indices / nrows).astype(int)

# assign NaN to each of the indices in df
for ri, ci in zip(row_indices, col_indices):
  df.iloc[ri, ci] = np.nan
Leave a Comment