Answers for "shuffle dataframe rows pandas"

2

python randomly shuffle rows of pandas dataframe

# Basic syntax:
df = df.sample(frac=1, random_state=1).reset_index(drop=True)
# Where:
#	- frac=1 specifies returning 100% of the original rows of the 
#		dataframe (in random order). Change to a decimal (e.g. 0.5) if
#		you want to sample say, 50% of the original rows
#	- random_state=1 sets the seed for the random number generator and
#		is useful to specify if you want results to be reproducible
#	- .reset_index(drop=True) specifies resetting the row index of the
#		shuffled dataframe
Posted by: Guest on November-12-2020
0

randomly shuffle pandas dataframe

  shuffled_train_df = train_df.reindex(np.random.permutation(train_df.index))
Posted by: Guest on July-09-2021

Code answers related to "shuffle dataframe rows pandas"

Python Answers by Framework

Browse Popular Code Answers by Language