Untitled

 avatar
unknown
plain_text
a year ago
561 B
4
Indexable
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
Editor is loading...
Leave a Comment