Subset Rows & Columns of pandas DataFrame in Python (2 Examples)

In this tutorial you’ll learn how to subset the rows and columns of a pandas DataFrame in Python.

Setting up the Examples

import pandas as pd                       # Load pandas library
my_df = pd.DataFrame({'A':range(1, 6),    # Constructing a pandas DataFrame
                      'B':range(7, 12),
                      'C':range(13, 18),
                      'D':range(19, 24)})
print(my_df)
#    A   B   C   D
# 0  1   7  13  19
# 1  2   8  14  20
# 2  3   9  15  21
# 3  4  10  16  22
# 4  5  11  17  23

Example 1: Using a Logical Condition to Subset a pandas DataFrame

df1 = my_df.loc[my_df['B'] <= 9]          # Create pandas DataFrame subset
print(df1)
#    A  B   C   D
# 0  1  7  13  19
# 1  2  8  14  20
# 2  3  9  15  21

Example 2: Using a Random Process to Subset a pandas DataFrame

import numpy                              # Import numpy
numpy.random.seed(23466463)               # Create pandas DataFrame subset
df2 = my_df.sample(frac = 0.75)
print(df2)
#    A   B   C   D
# 1  2   8  14  20
# 4  5  11  17  23
# 3  4  10  16  22
# 2  3   9  15  21

Further Resources & Related Articles

Here, you can find some additional resources on topics such as counting, variables, and lists.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top