You can use the sample method*:
In [11]: df = pd.DataFrame([[1, 2], [3, 4], [5, 6], [7, 8]], columns=["A", "B"])
In [12]: df.sample(2)
Out[12]:
A B
0 1 2
2 5 6
In [13]: df.sample(2)
Out[13]:
A B
3 7 8
0 1 2
*On one of the section DataFrames.
Note: If you have a larger sample size that the size of the DataFrame this will raise an error unless you sample with replacement.
In [14]: df.sample(5)
ValueError: Cannot take a larger sample than population when 'replace=False'
In [15]: df.sample(5, replace=True)
Out[15]:
A B
0 1 2
1 3 4
2 5 6
3 7 8
1 3 4
Answer from Andy Hayden on Stack OverflowPandas
pandas.pydata.org โบ docs โบ reference โบ api โบ pandas.DataFrame.sample.html
pandas.DataFrame.sample โ pandas 3.0.5 documentation
Generates random samples from each group of a DataFrame object.
Top answer 1 of 5
74
You can use the sample method*:
In [11]: df = pd.DataFrame([[1, 2], [3, 4], [5, 6], [7, 8]], columns=["A", "B"])
In [12]: df.sample(2)
Out[12]:
A B
0 1 2
2 5 6
In [13]: df.sample(2)
Out[13]:
A B
3 7 8
0 1 2
*On one of the section DataFrames.
Note: If you have a larger sample size that the size of the DataFrame this will raise an error unless you sample with replacement.
In [14]: df.sample(5)
ValueError: Cannot take a larger sample than population when 'replace=False'
In [15]: df.sample(5, replace=True)
Out[15]:
A B
0 1 2
1 3 4
2 5 6
3 7 8
1 3 4
2 of 5
12
One solution is to use the choice function from numpy.
Say you want 50 entries out of 100, you can use:
import numpy as np
chosen_idx = np.random.choice(1000, replace=False, size=50)
df_trimmed = df.iloc[chosen_idx]
This is of course not considering your block structure. If you want a 50 item sample from block i for example, you can do:
import numpy as np
block_start_idx = 1000 * i
chosen_idx = np.random.choice(1000, replace=False, size=50)
df_trimmed_from_block_i = df.iloc[block_start_idx + chosen_idx]
03:55
Randomly sampling a Pandas dataframe - YouTube
06:20
Getting a random sample from your pandas data frame - YouTube
02:49
Video 19: Randomly Sample Your Data Using Pandas - YouTube
09:45
21. Pandas Sampling DataFrame - random rows selection and grouping ...
05:08
Pandas Sample | pd.DataFrame.sample() - YouTube
03:04
How to use Pandas to Randomly Select Rows from DataFrame - YouTube
Pandas
pandas.pydata.org โบ pandas-docs โบ stable โบ reference โบ api โบ pandas.DataFrame.sample.html
pandas.DataFrame.sample โ pandas 3.0.4 documentation
Generates random samples from each group of a DataFrame object.
Pythontic
pythontic.com โบ pandas โบ dataframe-manipulations โบ sample selection
Creating a random sample from a pandas DataFrame | Pythontic.com
The pandas DataFrame class provides the method sample() that returns a random sample from the DataFrame. drop-truncate ยท duplicates-and-missing-values ยท explode ยท head-tail ยท querying a dataframe ยท replace elements ยท Copyright 2026 ยฉ pythontic.com
ProjectPro
projectpro.io โบ recipes โบ randomly-sample-pandas-dataframe
How to randomly sample a Pandas DataFrame? -
June 2, 2022 - We have created a dictionary of data and passed it in pd.DataFrame to make a dataframe with columns 'first_name', 'last_name', 'age', 'Comedy_Score' and 'Rating_Score'. raw_data = {'first_name': ['Sheldon', 'Raj', 'Leonard', 'Howard', 'Amy'], 'last_name': ['Copper', 'Koothrappali', 'Hofstadter', 'Wolowitz', 'Fowler'], 'age': [42, 38, 36, 41, 35], 'Comedy_Score': [9, 7, 8, 8, 5], 'Rating_Score': [25, 25, 49, 62, 70]} df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age', 'Comedy_Score', 'Rating_Score']) print(df) We can select random subsets of rows by df.take and passing random permutation of number from the length of df. We have done this twice for 2 and 4 samples to select.
Ryan Nolan Data
ryanandmattdatascience.com โบ home โบ python pandas โบ pandas sample
Pandas sample(): Random Sampling Made Simple in Python
June 15, 2025 - Learn how to use pandas sample() to randomly select rows from a DataFrame. Includes examples for sampling with/without replacement and setting random state.
pandas
pandas.pydata.org โบ pandas-docs โบ dev โบ reference โบ api โบ pandas.DataFrame.sample.html
pandas.DataFrame.sample โ pandas 3.1.0.dev0+974.ge652ee88a5 documentation
Generates random samples from each group of a DataFrame object.
Pandas
pandas.pydata.org โบ docs โบ dev โบ reference โบ api โบ pandas.DataFrame.sample.html
pandas.DataFrame.sample โ pandas documentation
Generates random samples from each group of a DataFrame object.
W3Schools
w3schools.com โบ python โบ pandas โบ ref_df_sample.asp
Pandas DataFrame sample() Method
Pandas HOME Pandas Intro Pandas Getting Started Pandas Series Pandas DataFrames Pandas Read CSV Pandas Read JSON Pandas Analyzing Data ยท Cleaning Data Cleaning Empty Cells Cleaning Wrong Format Cleaning Wrong Data Removing Duplicates ... Return one random sample row of the DataFrame.
IncludeHelp
includehelp.com โบ python โบ random-sample-of-a-subset-of-a-dataframe-in-pandas.aspx
Python - Random Sample of a subset of a dataframe in Pandas
October 3, 2023 - # Importing pandas package import pandas as pd # Creating a list l = [[1, 2], [3, 4], [5, 6], [7, 8]] # Creating a DataFrame df = pd.DataFrame(l,columns=['A','B']) # Display original DataFrame print("Original Dataframe:\n",df,"\n") # Getting a sample res = df.sample(2) # Display this random sample print("Sample of subset:\n",res,"\n")