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 `sample()`

function

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.

## Examples

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.

### 1. Sample rows based on count

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)
```

Output:

```
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`

.

### 2. Sample rows based on fraction

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)
```

Output:

```
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.

### 3. Sample rows with replacement

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)
```

Output:

```
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 –

- Pandas – Sort a DataFrame
- Change Order of Columns of a Pandas DataFrame
- Pandas DataFrame to a List in Python
- Pandas – Count of Unique Values in Each Column
- Pandas – Replace Values in a DataFrame
- Pandas – Filter DataFrame for multiple conditions
- Pandas – Random Sample of Rows
- Pandas – Random Sample of Columns
- Save Pandas DataFrame to a CSV file
- Pandas – Save DataFrame to an Excel file
- Create a Pandas DataFrame from Dictionary
- Convert Pandas DataFrame to a Dictionary
- Drop Duplicates from a Pandas DataFrame
- Concat DataFrames in Pandas
- Append Rows to a Pandas DataFrame
- Compare Two DataFrames for Equality in Pandas
- Get Column Names as List in Pandas DataFrame
- Select One or More Columns in Pandas
- Pandas – Rename Column Names
- Pandas – Drop one or more Columns from a Dataframe
- Pandas – Iterate over Rows of a Dataframe
- How to Reset Index of a Pandas DataFrame?
- Read CSV files using Pandas – With Examples
- Apply a Function to a Pandas DataFrame

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**