Here you can clear exactly 10% of the cells (more precisely, up to 10%, which can be achieved using the existing data frame size).
import random
ix = [(row, col) for row in range(df.shape[0]) for col in range(df.shape[1])]
for row, col in random.sample(ix, int(round(.1*len(ix)))):
df.iat[row, col] = np.nan
Here's a way to clean cells independently with a 10% chance of each cell.
df = df.mask(np.random.random(df.shape) < .1)
source
share