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 :: create a directory python 
Python :: python open encoding utf-8 
Python :: django create empty migration 
Python :: how to install drivers for selenium python 
Python :: classification report scikit 
Python :: how to add button in tkinter 
Python :: python cd to directory 
Python :: keras model load 
Python :: tkinter listbox delete all items 
Python :: python all possible combinations of multiple lists 
Python :: Calculate median with pyspark 
Python :: show image in python 
Python :: flask get ip address of request 
Python :: how to remember to put a semicolon after your code 
Python :: split string every n characters python 
Python :: remove first row of dataframe 
Python :: import status in django rest framework 
Python :: array of random integers python 
Python :: how to count docx pages python 
Python :: python ping ip address 
Python :: print first dictionary keys python 
Python :: python code to convert all keys of dict into lowercase 
Python :: remove whitespace around figure matplotlib 
Python :: python iterate dictionary in reverse order 
Python :: python randomise between 0 or 1 
Python :: how to install wxpython 
Python :: python most common element in list 
Python :: python pi value 
Python :: pandas dataframe from dict 
Python :: how to find wifi password using python 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =