How to use 'shuffle dataframe' in Python

Every line of 'shuffle dataframe' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
114def shuffleDataFrame(df):
115 """ Shuffles the rows of a dataframe """
116 df.reset_index(inplace=True)
117 del df['index']
118 return df.reindex(np.random.permutation(df.index))
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

29def shuffle(df, n=1):
30 ind = df.index
31 sampler = np.random.permutation
32 for i in range(n):
33 new_vals = df.take(sampler(df.shape[0])).values
34 df = pd.DataFrame(new_vals, index=ind)
35 return df

Related snippets