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 Overflow
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ pandas
pandas: Random sampling from DataFrame with sample() | note.nkmk.me
May 22, 2022 - You can get a random sample from pandas.DataFrame and Series by the sample() method.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ python โ€บ third-party โ€บ pandas โ€บ DataFrame โ€บ sample
Python Pandas DataFrame sample() - Random Row or Column Selection | Vultr Docs
December 24, 2024 - The sample() function in the Pandas library offers a powerful way to randomly select rows or columns from a DataFrame, which can be crucial for creating training and testing datasets, or for performing data integrity checks.
๐ŸŒ
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
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-randomly-select-rows-from-pandas-dataframe
Randomly Select Rows from Pandas DataFrame - GeeksforGeeks
October 3, 2025 - Example: Below, we randomly select one row using sample(). ... Returns a DataFrame with the sampled row.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Plain English
python.plainenglish.io โ€บ data-sampling-using-pandas-a-guide-to-beginners-1c0e107b2810
Data Sampling Using Pandas! A Guide to Beginners! | by AI TutorMaster | Python in Plain English
June 15, 2023 - import pandas as pd df = pd.DataFrame({'col1': [1, 2, 3, 4, 5], 'col2': [6, 7, 8, 9, 10]}) # generate a random sample of size 3 from the dataframe df.sample(n=3, random_state=42) ... New Python content every day.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-randomly-select-rows-from-pandas-dataframe
How to Randomly Select rows from Pandas DataFrame - GeeksforGeeks
April 15, 2025 - With a given DataFrame, the sample will always fetch same rows. If random_state is None or np.random, then a randomly-initialized RandomState object is returned. ... We can also use NumPy to randomly select rows based on their index.
๐ŸŒ
Data Science Parichay
datascienceparichay.com โ€บ home โ€บ blog โ€บ pandas โ€“ random sample of rows
Pandas - Random Sample of Rows - Data Science Parichay
October 22, 2020 - The pandas dataframe sample() function can be used to randomly sample rows from a pandas 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")
๐ŸŒ
DataScience Made Simple
datasciencemadesimple.com โ€บ home โ€บ random sampling in pandas python โ€“ random n rows
random sampling in pandas python - random n rows - DataScience Made Simple
November 15, 2019 - ''' 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