First time here? Checkout the FAQ!
x
+1 vote
342 views
asked in Python by (140 points)  
  

1 Answer

+1 vote
answered by (116k points)  

You can create a random DataFrame, then generate random column numbers and replace those columns for each row with NaN. You can run it here.

import pandas as pd
import numpy as np
import random

rows = 5
cols = 10 
nan_n = 5

df = pd.DataFrame(np.random.randint(0,100,size=(rows, cols)))

print(df)
for row in range(0,rows):
    nan_cols = np.asarray(random.sample(range(0, 10), nan_n))
    df.at[row, nan_cols] = np.nan

print(df)

 

...