741 views

1 Answer

1 1 vote

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)

 

Related questions

1 1 vote
1 answers 1 answer
5.9k
5.9k views
1 1 vote
1 1 answer
2.8k
2.8k views
tofighi asked Feb 27, 2020
2,766 views
How can I give access to others to view, comment or edit my Jupyter Notebook on Google Colab?
1 1 vote
1 1 answer
4.0k
4.0k views
askpython asked Oct 30, 2020
4,049 views
Whener I run Jupyter notebook there are some kernels that do not exist on system and generate errors. How can I remove them?
0 0 votes
1 1 answer
899
899 views
4 4 votes
1 1 answer
20.0k
20.0k views