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

python randomize a dataframe pandas

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

randomly shuffle pandas dataframe

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

PREVIOUS NEXT
Code Example
Python :: camel case in python 
Python :: merge dicts python 
Python :: sqlalchemy one to many 
Python :: remove multiindex pandas 
Python :: python tkinter text get 
Python :: deep copy a dataframe 
Python :: pandas count nans in column 
Python :: find min and max from dataframe column 
Python :: list exclude list 
Python :: adding proxy in selenium python 
Python :: What is role of ALLOWED_HOSTs in Django 
Python :: python json web request 
Python :: map and filter in python 
Python :: auto slug field django 
Python :: python package for misspelled words 
Python :: django or 
Python :: Exit code: ENOENT. spawn /usr/bin/python ENOENT 
Python :: word embedding python 
Python :: python message from teams 
Python :: python ordereddict 
Python :: dataframe python unique values rows 
Python :: buttons on canvas tkinter 
Python :: get last 3 in list python 
Python :: pandas create a new column based on condition of two columns 
Python :: oversampling using smote 
Python :: how to use input in python 
Python :: socketserver python 
Python :: how to read files in python with 
Python :: relative import in python 
Python :: how to select axis value in python 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =