Let us see how to shuffle the rows of a DataFrame. We will be using the
sample() method of the pandas module to to randomly shuffle DataFrame rows in Pandas.
- Import the
- Create a DataFrame.
- Shuffle the rows of the DataFrame using the
sample()method with the parameter
fracas 1, it determines what fraction of total instances need to be returned.
- Print the original and the shuffled DataFrames.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Shuffle a given Pandas DataFrame rows
- Python | Delete rows/columns from DataFrame using Pandas.drop()
- How to randomly select rows from Pandas DataFrame
- How to get rows/index names in Pandas dataframe
- Get all rows in a Pandas DataFrame containing given substring
- Different ways to iterate over rows in Pandas Dataframe
- Selecting rows in pandas DataFrame based on conditions
- How to iterate over rows in Pandas Dataframe
- Sorting rows in pandas DataFrame
- Dealing with Rows and Columns in Pandas DataFrame
- Iterating over rows and columns in Pandas DataFrame
- Ranking Rows of Pandas DataFrame
- Create a list from rows in Pandas dataframe
- Create a list from rows in Pandas DataFrame | Set 2
- How to Drop Rows with NaN Values in Pandas DataFrame?
- Drop rows from Pandas dataframe with missing values or NaN in columns
- How to drop rows in Pandas DataFrame by index labels?
- Get the number of rows and number of columns in Pandas Dataframe
- How to create an empty DataFrame and append rows & columns to it in Pandas?
- Apply a function to single or selected columns or rows in Pandas Dataframe
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.