As @user48956 commented on accepted answer, is much faster to sample over the index using numpy.random.choice

np.random.seed(42)
df = pd.DataFrame(np.random.randint(0,100,size=(10000000, 4)), columns=list('ABCD'))
%time df.sample(100000).index
print(_)
%time pd.Index(np.random.choice(df.index, 100000))
Wall time: 710 ms
Int64Index([7141956, 9256789, 1919656, 2407372, 9181191, 2474961, 2345700,
            4394530, 8864037, 6096638,
            ...
             471501, 3616956, 9397742, 6896140,  670892, 9546169, 4146996,
            3465455, 7748682, 5271367],
           dtype='int64', length=100000)
Wall time: 6.05 ms

Int64Index([7141956, 9256789, 1919656, 2407372, 9181191, 2474961, 2345700,
            4394530, 8864037, 6096638,
            ...
             471501, 3616956, 9397742, 6896140,  670892, 9546169, 4146996,
            3465455, 7748682, 5271367],
           dtype='int64', length=100000)
Answer from Ameb on Stack Overflow
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.sample.html
pandas.DataFrame.sample — pandas 3.0.5 documentation
This is similar to specifying n=4 without replacement on a Series with 3 elements. ... >>> 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"], ...
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.index.html
pandas.DataFrame.index — pandas 3.0.4 documentation
In this example, we create a DataFrame with 3 rows and 3 columns, including Name, Age, and Location information. We set the index labels to be the integers 10, 20, and 30.
🌐
Spark By {Examples}
sparkbyexamples.com › home › pandas › pandas index explained with examples
Pandas Index Explained with Examples - Spark By {Examples}
June 27, 2025 - Since we have not given labels to columns and rows(index), DataFrame by default assigns incremental sequence numbers as labels to both rows and columns called Index. # Output: 0 1 2 0 Spark 20000 30days 1 pandas 20000 40days
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › user_guide › indexing.html
Indexing and selecting data — pandas 3.0.5 documentation
In both cases, start and stop determine the label boundaries (inclusive), while step skips positions within that range, regardless of the index type. pandas provides a suite of methods in order to get purely integer based indexing. The semantics follow closely Python and NumPy slicing.
🌐
Programiz
programiz.com › python-programming › pandas › index
Pandas Index (With Examples)
Original DataFrame: Name Age City ... property. For example, import pandas as pd # create a dataframe data = {'Name': ['John', 'Alice', 'Bob'], 'Age': [25, 28, 32], 'City': ['New York', 'London', 'Paris']} df = pd.DataFrame(data)...
🌐
Medium
medium.com › @swamy.annamalai › index-in-pandas-dataframe-bfa35382b7fb
Index in Pandas DataFrame. With Python Samples. | by Annamalai Swamy | Medium
December 2, 2023 - If rows does not have named indexes, then index property returns a Range index object with start, stop and step values
🌐
GeeksforGeeks
geeksforgeeks.org › python-pandas-series-sample
Python | Pandas Series.sample() | GeeksforGeeks
February 7, 2019 - The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Pandas Series.sample() function return a random sample of items from an axis of object.
Find elsewhere
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.sample.html
pandas.DataFrame.sample — pandas 2.2.3 documentation
Generates a random sample from a given 1-D numpy array. ... If frac > 1, replacement should be set to True. ... >>> 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 ...
🌐
Dataquest
dataquest.io › home › blog › tutorial: indexing dataframes in pandas
Python Tutorial: How to Index DataFrames in Pandas (2022) %%sep%% %%sitename%%
December 20, 2024 - The position numbers are integers starting from 0 for the first row or column and increasing by 1 for each subsequent row/column, so they can also be used as unique coordinates of particular dataframe elements (rows, columns, or data points). This ability to refer dataframe elements by label or position number is exactly what makes dataframe indexing possible. A specific (and virtually, the most common) case of pandas dataframe indexing is slicing.
🌐
Dask
docs.dask.org › en › stable › generated › dask.dataframe.Index.sample.html
dask.dataframe.Index.sample — Dask documentation
Random sample of items · Number of items to return is not supported by dask. Use frac instead
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Index.html
pandas.Index — pandas 3.0.5 documentation
Immutable sequence used for indexing and alignment. The basic object storing axis labels for all pandas objects.
🌐
pandas
pandas.pydata.org › pandas-docs › dev › reference › api › pandas.DataFrame.sample.html
pandas.DataFrame.sample — pandas 3.1.0.dev0+974.ge652ee88a5 documentation
This is similar to specifying n=4 without replacement on a Series with 3 elements. ... >>> 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"], ...
🌐
Scaler
scaler.com › home › topics › what is pandas dataframe sample() method?
What is Pandas DataFrame sample() Method? - Scaler Topics
May 4, 2023 - If set to True, the pandas sample() method can return the same item more than once. weights – determines the influence of specified axis items (rows or columns) on the result of sampling. By default, all axis items have equal weights. The weights parameter can be set to a string, a list, or a Series. Except for the last case, where the provided Series is aligned with the original object based on the index...
🌐
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.DataFrame.sample.html
pandas.DataFrame.sample — pandas 3.0.4 documentation
This is similar to specifying n=4 without replacement on a Series with 3 elements. ... >>> 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"], ...
🌐
IONOS
ionos.com › digital guide › websites › web development › python pandas: dataframe indexing
How to index pandas DataFrames - IONOS
May 30, 2025 - To retrieve the data of more than one column, write the column names in the indexing operator and place commas between them to separate them. If you need to access a specific row in your DataFrame, you can use the pandas loc indexer. You can pass the row label or row number to the indexer.
🌐
w3resource
w3resource.com › pandas › series › series-sample.php
Pandas Series: sample() function - w3resource
May 13, 2026 - Example - A random 50% sample of the DataFrame with replacement: Python-Pandas Code: import numpy as np import pandas as pd df = pd.DataFrame({'num_legs': [2, 4, 8, 0], 'num_wings': [2, 0, 0, 0], 'num_specimen_seen': [8, 2, 1, 6]}, index=['sparrow', 'cat', 'spider', 'snake']) df.sample(frac=0.5, replace=True, random_state=1) Output: num_legs num_wings num_specimen_seen cat 4 0 2 snake 0 0 6 ·
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: Random sampling from DataFrame with sample() | note.nkmk.me
May 22, 2022 - pandas.Series.sample — pandas 1.4.2 documentation · This article describes the following contents. Default behavior of sample() Rows or columns: axis · The number of rows and columns: n · The fraction of rows and columns: frac · The seed for the random number generator: random_state · With or without replacement: replace · Reset index: ignore_index, reset_index() Use the iris data set included as a sample in seaborn.
🌐
pandas
pandas.pydata.org › pandas-docs › dev › reference › api › pandas.DataFrame.index.html
pandas.DataFrame.index — pandas 3.1.0.dev0+1386.gcb2086a1a4 documentation
In this example, we create a DataFrame with 3 rows and 3 columns, including Name, Age, and Location information. We set the index labels to be the integers 10, 20, and 30.
🌐
datagy
datagy.io › home › pandas tutorials › pandas dataframes › pandas drop a dataframe index column: guide with examples
Pandas Drop a Dataframe Index Column: Guide with Examples • datagy
September 3, 2023 - Note: While indices technically exist across the DataFrame columns as well (i.e., along axis 1), when this article refers to an index, I’m only referring to the row index. To follow along with this tutorial, I have provided a sample Pandas DataFrame below.