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 Top answer 1 of 2
6
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)
2 of 2
5
Let's try and use .index after sample to return the indexes of those samples:
sample_dict[user] = series[series==user].sample(n=sample_size, replace=False).index
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.sample.html
pandas.DataFrame.sample — pandas 3.0.5 documentation
E.g. sampling 2 items without replacement with weights [100, 1, 1] would yield two last items in 1/2 of cases, instead of 1/102. 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"], ...
16:13
Select Random Rows or Columns in Pandas with sample() – Quick ...
11:22
Indexes in Pandas | Python Pandas Tutorials - YouTube
05:08
Pandas Sample | pd.DataFrame.sample() - YouTube
12:19
#14. Pandas Sampling a Series and DataFrame in Python | Tutorial ...
06:20
Getting a random sample from your pandas data frame - YouTube
Pandas
pandas.pydata.org › pandas-docs › stable › reference › api › pandas.DataFrame.sample.html
pandas.DataFrame.sample — pandas 3.0.4 documentation
E.g. sampling 2 items without replacement with weights [100, 1, 1] would yield two last items in 1/2 of cases, instead of 1/102. 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"], ...
w3resource
w3resource.com › pandas › series › series-sample.php
Pandas Series: sample() function - w3resource
May 13, 2026 - Rows with larger value in the num_specimen_seen column are more likely to be sampled: ... 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(n=2, weights='num_specimen_seen', random_state=1)
pandas
pandas.pydata.org › pandas-docs › dev › reference › api › pandas.DataFrame.sample.html
pandas.DataFrame.sample — pandas 3.1.0.dev0+974.ge652ee88a5 documentation
E.g. sampling 2 items without replacement with weights [100, 1, 1] would yield two last items in 1/2 of cases, instead of 1/102. 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"], ...
datagy
datagy.io › home › pandas tutorials › pandas dataframes › 7 ways to sample data in pandas
7 Ways to Sample Data in Pandas • datagy
December 19, 2022 - Check out my tutorial here, which will teach you different ways of calculating the square root, both without Python functions and with the help of functions. You may also want to sample a Pandas Dataframe using a condition, meaning that you can return all rows the meet (or don’t meet) a certain condition. In order to filter our dataframe using conditions, we use the [] square root indexing method, where we pass a condition into the square roots.
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 ...
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.index.html
pandas.DataFrame.index — pandas 3.0.5 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.
Pandas
pandas.pydata.org › docs › user_guide › indexing.html
Indexing and selecting data — pandas 3.0.5 documentation
pandas provides a suite of methods in order to get purely integer based indexing. The semantics follow closely Python and NumPy slicing. These are 0-based indexing. When slicing, the start bound is included, while the upper bound is excluded.
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'], ...
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, weights must be of the same length as the axis being sampled.
Pandas
pandas.pydata.org › docs › dev › reference › api › pandas.DataFrame.sample.html
pandas.DataFrame.sample — pandas documentation
E.g. sampling 2 items without replacement with weights [100, 1, 1] would yield two last items in 1/2 of cases, instead of 1/102. 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"], ...
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.
Pandas
pandas.pydata.org › pandas-docs › version › 1.5 › reference › api › pandas.DataFrame.sample.html
pandas.DataFrame.sample — pandas 1.5.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 critical difference between label-based and position-based dataframe indexing approaches is in the way of dataframe slicing: for the position-based indexing, it is purely Python-style, i.e, the start bound of the range is inclusive while the stop bound is exclusive. For the label-based indexing, the stop bound is inclusive. To retrieve all data from multiple sequential rows of a pandas dataframe, we can simply use the indexing operator [] and a range of the necessary row positions (it can be an open-ending range):
Dask
docs.dask.org › en › stable › generated › dask.dataframe.Index.sample.html
dask.dataframe.Index.sample — Dask documentation
Approximate fraction of items to return. This sampling fraction is applied to all partitions equally. Note that this is an approximate fraction.