python randomize a dataframe pandas
# 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
0
0
df = df.sample(frac=1).reset_index(drop=True)
Thank you!
0
0
Are there any code examples left?
New code examples in category Python
-
Python 2023-04-11 03:04:20
-
Python 2022-03-27 22:40:04 pycharm no module named
-
Python 2022-03-27 22:25:05 assign multiple variablesin one line
-
Python 2022-03-27 22:20:02 levenshtein distance
-
Python 2022-03-27 21:35:09 get text from url python last slash
-
Python 2022-03-27 21:30:30 df concatenate df
-
Python 2022-03-27 21:25:09 python odd or even
-
Python 2022-03-27 21:15:32 python include function from another file
-
Python 2022-03-27 21:10:01 color module python
-
Python 2022-03-27 21:00:27 python tkinter cursor types