Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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
Comment

pandas shuffle rows

df = df.sample(frac=1).reset_index(drop=True)
Comment

shuffle rows dataframe

df = df.sample(frac=1).reset_index(drop=True) #Use if you want to reset index order

df.sample(frac=1) # Use for no reset in index order
Comment

randomly shuffle pandas dataframe

  shuffled_train_df = train_df.reindex(np.random.permutation(train_df.index))
Comment

PREVIOUS NEXT
Code Example
Python :: linux python installation wheel 
Python :: generate a color python 
Python :: download pdf from link using python 
Python :: base64 encode python 
Python :: verify django has been installed 
Python :: pandas tuple from two columns 
Python :: OMP: Error #15: Initializing libomp.a, but found libiomp5.dylib already initialized. 
Python :: how to check if column has na python 
Python :: pandas remove char from column 
Python :: open pkl file python 
Python :: pdb set trace 
Python :: Write a line to a text file using the write() function 
Python :: get current date and time with python 
Python :: how to make my jupyter prin full array 
Python :: python pandas dataframe column date to string 
Python :: how to import login required in django 
Python :: python play sound 
Python :: numpy documentation 
Python :: remove all 0 from list python 
Python :: make y axis start at 0 python 
Python :: how to send a message in a specific channel discord.py 
Python :: how to read tsv file python 
Python :: python hand tracking module 
Python :: plot function in numpy 
Python :: install python 3.9 linux 
Python :: jupyter print full dataframe 
Python :: user agents list 
Python :: how to get random word from text file in python 
Python :: python execute string 
Python :: how to get a random element from an array in python 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =