Selection of random n rows in pandas python is carried out using sample() function. So this is nothing but simple random sampling in pandas.
Let’s see how to
- Select random n rows in pandas python.
- Select random n% of rows in pandas dataframe – python
With an example for each. First let’s create a dataframe.
import pandas as pd
import numpy as np
#Create a DataFrame
df1 = {
'Name':['George','Andrea','micheal','maggie','Ravi','Xien','Jalpa'],
'Mathematics_score':[62,47,55,74,32,77,86]}
df1 = pd.DataFrame(df1,columns=['Name','Mathematics_score'])
print(df1)
df1 will be

Select random n rows in a pandas python (random sampling in pandas):
Random n rows of a dataframe is selected using sample function and with argument n as number of rows as shown below.
''' Random sampling - Random n rows ''' df1_elements = df1.sample(n=4) print(df1_elements)
so the resultant dataframe will select 4 random rows from dataframe df1

Select random n% rows in a pandas dataframe python
Random n% of rows in a dataframe is selected using sample function and with argument frac as percentage of rows as shown below.
''' Random sampling - Random n% rows ''' df1_percent = df1.sample(frac=0.7) print(df1_percent)
so the resultant dataframe will select 70% of rows randomly from dataframe df1.






