1

My data sheet has 250 columns with column names '1'...'250'. After random sampling each column, I want to save all the sampled columns in a single csv file with same column names as in the data sheet.

for i in range(1,250):
   z=np.random.choice(df[i], len(df), replace=False)

How to proceed?

1 Answer 1

1

I believe need assign back each column and then write to csv by to_csv:

for i in range(1,250):
   df[i]=np.random.choice(df[i], len(df), replace=False)

df.to_csv(file, index=False)

Another solution with sample:

df1 = df.sample(len(df.columns), replace=False)
df.to_csv(file, index=False)

Sample:

df = pd.DataFrame(np.random.randint(30, size=(5,4))).rename(columns=lambda x: x+1)
print (df)
    1   2   3   4
0  12  10   2  14
1   9  14  28   4
2   9  11  14   8
3  22   8   2   9
4  28   3  23   6

df1 = df.sample(len(df.columns), axis=0, replace=False)
print (df1)
    1   2   3   4
3  22   8   2   9
0  12  10   2  14
2   9  11  14   8
4  28   3  23   6

for i in range(1,5):
   df[i]=np.random.choice(df[i], len(df), replace=False)

print (df)
    1   2   3   4
0  28  11  23   8
1  22   3   2  14
2   9  10  28   4
3   9   8  14   9
4  12  14   2   6
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.