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
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.sample.html
pandas.DataFrame.sample — pandas 3.0.5 documentation
>>> df.sample(frac=2, replace=True, random_state=1) num_legs num_wings num_specimen_seen dog 4 0 2 fish 0 0 8 falcon 2 2 10 falcon 2 2 10 fish 0 0 8 dog 4 0 2 fish 0 0 8 dog 4 0 2 · Using a DataFrame column as weights.
🌐
W3Schools
w3schools.com › python › pandas › ref_df_sample.asp
Pandas DataFrame sample() Method
A DataFrame with a sample of the original DataFrame. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python ...
🌐
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 - Since the original DataFrame has 4 rows, it returns 2 rows randomly. For reproducible results, use the random_state parameter. Pass an integer to random_state which acts as the seed for the random number generator. ... # Sampling with a seed for reproducibility sample_seed = df.sample(n=2, random_state=1) print(sample_seed) Explain Code
🌐
Apache
spark.apache.org › docs › latest › api › python › reference › pyspark.pandas › api › pyspark.pandas.DataFrame.sample.html
pyspark.pandas.DataFrame.sample — PySpark 4.1.2 documentation
A new object of same type as caller containing the sampled items. ... >>> df = ps.DataFrame({'num_legs': [2, 4, 8, 0], ... 'num_wings': [2, 0, 0, 0], ... 'num_specimen_seen': [10, 2, 1, 8]}, ... index=['falcon', 'dog', 'spider', 'fish'], ...
🌐
Medium
medium.com › data-science › how-to-sample-a-dataframe-in-python-pandas-d18a3187139b
How to Sample a Dataframe in Python Pandas | by Angelica Lo Duca | TDS Archive | Medium
July 7, 2021 - Given a dataframe with N rows, random Sampling extract X random rows from the dataframe, with X ≤ N. Python pandas provides a function, named sample() to perform random sampling.
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: Random sampling from DataFrame with sample() | note.nkmk.me
May 22, 2022 - pandas: Reset index of DataFrame, Series with reset_index() print(df.sample(n=3).reset_index(drop=True)) # sepal_length sepal_width petal_length petal_width species # 0 4.9 3.1 1.5 0.2 setosa # 1 7.9 3.8 6.4 2.0 virginica # 2 6.3 2.8 5.1 1.5 virginica · source: pandas_sample.py · Python ·
Find elsewhere
🌐
w3resource
w3resource.com › pandas › dataframe › dataframe-sample.php
Pandas DataFrame: - sample() function - w3resource
August 19, 2022 - The sample() function is used to get a random sample of items from an axis of object. ... DataFrame.sample(self, n=None, frac=None, replace=False, weights=None, random_state=None, axis=None)
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 1.5 › reference › api › pandas.DataFrame.sample.html
pandas.DataFrame.sample — pandas 1.5.3 documentation
>>> df.sample(frac=2, replace=True, random_state=1) num_legs num_wings num_specimen_seen dog 4 0 2 fish 0 0 8 falcon 2 2 10 falcon 2 2 10 fish 0 0 8 dog 4 0 2 fish 0 0 8 dog 4 0 2 · Using a DataFrame column as weights.
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.22.0 › generated › pandas.DataFrame.sample.html
pandas.DataFrame.sample — pandas 0.22.0 documentation
Returns a random sample of items from an axis of object. ... >>> s = pd.Series(np.random.randn(50)) >>> s.head() 0 -0.038497 1 1.820773 2 -0.972766 3 -1.598270 4 -1.095526 dtype: float64 >>> df = pd.DataFrame(np.random.randn(50, 4), columns=list('ABCD')) >>> df.head() A B C D 0 0.016443 -2.318952 ...
🌐
Medium
medium.com › @prathik.codes › sampling-dataframes-sample-of-pandas-e94e94b4473b
Sampling DataFrames : sample() of Pandas | by Prathik C | Medium
March 3, 2025 - import pandas as pd import seaborn as sns df = sns.load_dataset('titanic') print(df.info()) # getting the 25% sample of the df rows sample_df_25 = df.sample(0.25) print(sample_df_25.info()) # getting sample of 2 columns from df sample_df_2 = df.sample(2,axis=1) print(sample_df_2.info()) ... # df.info <class 'pandas.core.frame.DataFrame'> RangeIndex: 891 entries, 0 to 890 Data columns (total 15 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 survived 891 non-null int64 1 pclass 891 non-null int64 2 sex 891 non-null object 3 age 714 non-null float64 4 sibsp 891 non-null
🌐
pandas
pandas.pydata.org › pandas-docs › dev › reference › api › pandas.DataFrame.sample.html
pandas.DataFrame.sample — pandas 3.1.0.dev0+974.ge652ee88a5 documentation
>>> df.sample(frac=2, replace=True, random_state=1) num_legs num_wings num_specimen_seen dog 4 0 2 fish 0 0 8 falcon 2 2 10 falcon 2 2 10 fish 0 0 8 dog 4 0 2 fish 0 0 8 dog 4 0 2 · Using a DataFrame column as weights.
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.DataFrame.sample.html
pandas.DataFrame.sample — pandas 3.0.4 documentation
>>> df.sample(frac=2, replace=True, random_state=1) num_legs num_wings num_specimen_seen dog 4 0 2 fish 0 0 8 falcon 2 2 10 falcon 2 2 10 fish 0 0 8 dog 4 0 2 fish 0 0 8 dog 4 0 2 · Using a DataFrame column as weights.
🌐
Programiz
programiz.com › python-programming › pandas › methods › sample
Pandas sample()
This is useful when we want to reproduce the same random sample in different runs of your code. Note: The choice of 42 as the seed value is arbitrary; we can use any integer value we like. import pandas as pd # create a DataFrame data = {'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'], 'Age': [25, 32, 28, 22, 30], 'City': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Miami']} df = pd.DataFrame(data)
🌐
Apache
spark.apache.org › docs › latest › api › python › reference › pyspark.sql › api › pyspark.sql.DataFrame.sample.html
pyspark.sql.DataFrame.sample — PySpark 4.2.0 documentation
This is not guaranteed to provide exactly the fraction specified of the total count of the given DataFrame. fraction is required and, withReplacement and seed are optional. ... >>> df = spark.range(0, 10, 1, 1) >>> df.sample(0.5, 3).count() 7 >>> df.sample(fraction=0.5, seed=3).count() 4 >>> df.sample(withReplacement=True, fraction=0.5, seed=3).count() 2 >>> df.sample(1.0).count() 10 >>> df.sample(fraction=1.0).count() 10 >>> df.sample(False, fraction=1.0).count() 10
🌐
Skytowner
skytowner.com › explore › pandas_dataframe_sample_method
Pandas DataFrame | sample method with Examples
Pandas DataFrame.sample(~) method returns the specified number of rows or columns randomly.
🌐
Snowflake Documentation
docs.snowflake.com › en › developer-guide › snowpark › reference › python › 1.17.0 › modin › pandas_api › snowflake.snowpark.modin.pandas.DataFrame.sample
modin.pandas.DataFrame.sample | Snowflake Documentation
>>> df = pd.DataFrame({'num_legs': [2, 4, 8, 0], ... 'num_wings': [2, 0, 0, 0], ... 'num_specimen_seen': [10, 2, 1, 8]}, ... index=['falcon', 'dog', 'spider', 'fish']) >>> df num_legs num_wings num_specimen_seen falcon 2 2 10 dog 4 0 2 spider 8 0 1 fish 0 0 8 ... >>> df.sample(n=20) num_legs num_wings num_specimen_seen falcon 2 2 10 dog 4 0 2 spider 8 0 1 fish 0 0 8