Let us see how to shuffle the rows of a DataFrame. We will be using the sample() method of the pandas module to randomly shuffle DataFrame rows in Pandas. Algorithm :
- Import the pandas and numpy modules.
- Create a DataFrame.
- Shuffle the rows of the DataFrame using the sample() method with the parameter frac as 1, it determines what fraction of total instances need to be returned.
- Print the original and the shuffled DataFrames.
python
# import the modulesimport pandas as pdimport numpy as np# create a DataFrameODI_runs = {'name': ['Tendulkar', 'Sangakkara', 'Ponting', 'Jayasurya', 'Jayawardene', 'Kohli', 'Haq', 'Kallis', 'Ganguly', 'Dravid'], 'runs': [18426, 14234, 13704, 13430, 12650, 11867, 11739, 11579, 11363, 10889]}df = pd.DataFrame(ODI_runs)# print the original DataFrameprint("Original DataFrame :")print(df)# shuffle the DataFrame rowsdf = df.sample(frac = 1)# print the shuffled DataFrameprint("\nShuffled DataFrame:")print(df) |
Output : 
