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 :: check if a value in dataframe is nan 
Python :: calculate entropy 
Python :: absolute value of int python 
Python :: valid parentheses with python 
Python :: how to create an empty 2d list in python 
Python :: check cuda available tensorflow 
Python :: get number of bits on integer in python 
Python :: matplotlib remove y axis label 
Python :: dataframe x y to geodataframe 
Python :: Python, pytorch math square 
Python :: send email hotmail using python 
Python :: how to make player quit in python 
Python :: django email settings 
Python :: epoch to datetime utc python 
Python :: django getting started 
Python :: create list in range 
Python :: producer consumer problem using queue python 
Python :: prime number program in python 
Python :: import static in django urls 
Python :: how to split a string in python with multiple delimiters 
Python :: dict godot 
Python :: not importing local folder python 
Python :: get number of string python 
Python :: pandas groupby aggregate quantile 
Python :: jupyter notebook check memory usage 
Python :: removing a channel from aconda 
Python :: pandas open text file 
Python :: flask hello world 
Python :: python get html info 
Python :: mongodb check if substring in string 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =