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
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-pandas-dataframe-sample
Pandas Dataframe.sample() | Python - GeeksforGeeks
July 11, 2025 - In this example, we generate a random sample consisting of 25% of the entire DataFrame by using the frac parameter. ... import pandas as pd d = pd.read_csv("employees.csv") # Sample 25% of the data sr = d.sample(frac=0.25) # Verify the number of rows print(f"Original rows: {len(d)}") print(f"Sampled rows (25%): {len(sr)}") # Display the result sr
🌐
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.
🌐
W3Schools
w3schools.com › python › pandas › ref_df_sample.asp
Pandas DataFrame sample() Method
import pandas as pd df = pd.read_csv('data.csv') print(df.sample()) Try it Yourself » · The sample() method returns a specified number of random rows. The sample() method returns 1 row if a number is not specified.
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-randomly-select-rows-from-pandas-dataframe
Randomly Select Rows from Pandas DataFrame - GeeksforGeeks
October 3, 2025 - Let’s explore different methods to randomly select rows from a Pandas DataFrame. The sample() method allows specifying the number of rows, a fraction of rows, whether to sample with replacement, weights and reproducibility via random_state.
🌐
Medium
medium.com › @bouimouass.o › df-sample-methode-a8a43e1bb7
df.sample() methode.
August 15, 2023 - df.sample() methode. The df.sample() method in Pandas is used to randomly sample rows from a DataFrame. The method takes a number of arguments that can be used to customize the behavior of the …
🌐
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 › 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.
🌐
Softhints
softhints.com › pandas-random-sample-of-a-subset-of-a-dataframe-rows-or-columns
Pandas - Random Sample of a subset of a DataFrame - rows or columns - Softhints
February 10, 2022 - col = 'color' for typ in list(df[col].dropna().unique()): print(typ, end=' - ') display(df[df[col] == typ].sample(3)) ... Color - 2867 Color .. 4345 Color .. 764 Color .. Black and White - 286 Black and White .. 3904 Black and White .. 1595 Black and White .. ... In case of more values in the column and if you like to get only one random row per kind from this DataFrame - you can use the code below:
🌐
Saturn Cloud
saturncloud.io › blog › random-row-selection-in-pandas-dataframe
Random Row Selection in Pandas Dataframe | Saturn Cloud Blog
May 1, 2026 - This method returns a random sample of rows from the dataframe, based on the specified number or fraction of rows. ... Name,Age,Score John,25,80 Jane,30,92 Bob,22,78 Alice,28,85 Charlie,35,90 David,32,88 Eva,27,95 Frank,24,82 Grace,29,89 Hank,26,91 ...
🌐
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.
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas dataframe sample() function
Pandas DataFrame sample() Function - Spark By {Examples}
July 29, 2024 - In pandas, the sample() function is used to generate a random sample of rows from a DataFrame. This function can be particularly useful for testing,
🌐
Programiz
programiz.com › python-programming › pandas › methods › sample
Pandas sample()
The sample() method in Pandas is used to randomly select a specified number of rows from a DataFrame.