Pandas dataframes are great for handling two dimensional tabular data. It may happen that you require to randomly select a subset of rows from a dataframe. In this tutorial we’ll look at how to get a random sample of rows of a pandas dataframe.
The pandas dataframe sample() function can be used to randomly sample rows from a pandas dataframe. It can sample rows based on a count or a fraction and provides the flexibility of optionally sampling rows with replacement. The following is its syntax:
df_subset = df.sample(n=num_rows)
Here df is the dataframe from which you want to sample the rows. The parameter n is used to determine the number of rows to sample. It defaults to 1. You can also sample rows based on fraction instead of a count using the frac parameter.
Note: Fix the random_state to get reproducible results.
First, we’ll create a sample dataframe that we’ll be using throughout this tutorial.
import pandas as pd data = < 'Name': ['Microsoft Corporation', 'Google, LLC', 'Tesla, Inc.',\ 'Apple Inc.', 'Netflix, Inc.'], 'Symbol': ['MSFT', 'GOOG', 'TSLA', 'AAPL', 'NFLX'], 'Shares': [100, 50, 150, 200, 80] >df = pd.DataFrame(data) df
Now, let’s look at some of the different use-cases of sampling rows from a dataframe via the pandas dataframe sample() function.
To randomly sample a fixed number of rows from a dataframe, pass the number of rows to sample to the n parameter of the sample() function.
df_sub = df.sample(n=2, random_state=2) print(df_sub)
📚 Data Science Programs By Skill Level
Introductory ⭐
Intermediate ⭐⭐⭐
Advanced ⭐⭐⭐⭐⭐
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Name Symbol Shares 2 Tesla, Inc. TSLA 150 4 Netflix, Inc. NFLX 80
In the above example, we randomly sample two rows from the dataframe df .
If you want to sample rows based on a fraction instead of a count, example, half of all the rows, you can use the frac parameter.
df_sub = df.sample(frac=0.4, random_state=2) print(df_sub)
Name Symbol Shares 2 Tesla, Inc. TSLA 150 4 Netflix, Inc. NFLX 80
In the above example, we sample 40% of rows of the dataframe df by passing the fraction 0.4 to the frac parameter.
The pandas dataframe sample() function also let’s you sample rows with replacement. Meaning, you can sample the same row more than once. To enable sampling rows with replacement, pass replace=True to the sample() function.
Note that the default behavior of the sample() function is to not sample with replacement. That is, the parameter replace is False by default.
df_sub = df.sample(n=3, replace=True, random_state=2) print(df_sub)
Name Symbol Shares 0 Microsoft Corporation MSFT 100 0 Microsoft Corporation MSFT 100 3 Apple Inc. AAPL 200
In the above example, you can see that the row with index 0 of the dataframe df is sampled twice. This happened because of sampling rows with replacement.
For more on the pandas dataframe sample() function. Refer to its official documentation.
With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5
More on Pandas DataFrames –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.
Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects. View all posts