How to Select Rows from Pandas DataFrame

Looking to select rows from pandas DataFrame?

If so, I’ll show you the steps to select rows from pandas DataFrame based on the conditions specified.

I’ll use simple examples to demonstrate this concept in Python.

Steps to Select Rows from Pandas DataFrame

Step 1: Gather your data-set

Firstly, you’ll need to gather your data. For illustration purposes, I gathered the following data about boxes:

ColorShapePrice
GreenRectangle10
GreenRectangle15
GreenSquare5
BlueRectangle5
BlueSquare10
RedSquare15
RedSquare15
RedRectangle5

Step 2: Create the DataFrame

Once you have your data ready, you’ll need to create the pandas DataFrame to capture that data in Python.

For our example, you may use the code below to create the DataFrame:

from pandas import DataFrame

Boxes = {'Color': ['Green','Green','Green','Blue','Blue','Red','Red','Red'],
         'Shape': ['Rectangle','Rectangle','Square','Rectangle','Square','Square','Square','Rectangle'],
         'Price': [10,15,5,5,10,15,15,5]
        }

df = DataFrame(Boxes, columns= ['Color','Shape','Price'])
print (df)

Run the code in Python and you would see this DataFrame:

Example of DataFrame

Step 3: Select Rows from Pandas DataFrame

You can use the following logic to select rows from pandas DataFrame based on specified conditions:

df.loc[df.column name condition]

For example, if you want to get the rows where the color is green, then you’ll need to apply:

df.loc[df.Color == ‘Green’]

Where:

  • Color is the column name
  • Green is the condition

And here is the full Python code for our example:

from pandas import DataFrame

Boxes = {'Color': ['Green','Green','Green','Blue','Blue','Red','Red','Red'],
         'Shape': ['Rectangle','Rectangle','Square','Rectangle','Square','Square','Square','Rectangle'],
         'Price': [10,15,5,5,10,15,15,5]
        }

df = DataFrame(Boxes, columns= ['Color','Shape','Price'])

select_color = df.loc[df.Color == 'Green']
print (select_color)

Once you run the code, you’ll get the rows where the color is green:

Select Rows from Pandas DataFrame

Additional Examples

Let’s now review additional examples to get a better sense of selecting rows from a pandas DataFrame.

Example 1: Select rows where the price is equal or greater than 10

To get all the rows where the price is equal or greater than 10, you’ll need to apply this condition:

df.loc[df.Price >= 10]

And this is the complete Python code:

from pandas import DataFrame

Boxes = {'Color': ['Green','Green','Green','Blue','Blue','Red','Red','Red'],
         'Shape': ['Rectangle','Rectangle','Square','Rectangle','Square','Square','Square','Rectangle'],
         'Price': [10,15,5,5,10,15,15,5]
        }

df = DataFrame(Boxes, columns= ['Color','Shape','Price'])

select_price = df.loc[df.Price >= 10]
print (select_price)

Run the code, and you’ll get all the rows where the price is equal or greater than 10:

Select Rows in DataFrame

Example 2: Select rows where the color is green AND the shape is rectangle

Now the goal is to select rows based on two conditions:

  • Color is green; and
  • Shape is rectangle

You may then use the & symbol to apply multiple conditions. In our example, the code would look like this:

df.loc[(df.Color == ‘Green’) & (df.Shape == ‘Rectangle’)]

Putting everything together:

from pandas import DataFrame

Boxes = {'Color': ['Green','Green','Green','Blue','Blue','Red','Red','Red'],
         'Shape': ['Rectangle','Rectangle','Square','Rectangle','Square','Square','Square','Rectangle'],
         'Price': [10,15,5,5,10,15,15,5]
        }

df = DataFrame(Boxes, columns= ['Color','Shape','Price'])

color_and_shape = df.loc[(df.Color == 'Green') & (df.Shape == 'Rectangle')]
print (color_and_shape)

Run the code and you’ll get the rows with the green color and rectangle shape:

Select Rows from Pandas DataFrame

Example 3: Select rows where the color is green OR the shape is rectangle

You can also select the rows based on one condition or another. For instance, you can select the rows if the color is green or the shape is rectangle.

To achieve this goal, you can use the | symbol as follows:

df.loc[(df.Color == ‘Green’) | (df.Shape == ‘Rectangle’)]

And here is the complete Python code:

from pandas import DataFrame

Boxes = {'Color': ['Green','Green','Green','Blue','Blue','Red','Red','Red'],
         'Shape': ['Rectangle','Rectangle','Square','Rectangle','Square','Square','Square','Rectangle'],
         'Price': [10,15,5,5,10,15,15,5]
        }

df = DataFrame(Boxes, columns= ['Color','Shape','Price'])

color_or_shape = df.loc[(df.Color == 'Green') | (df.Shape == 'Rectangle')]
print (color_or_shape)

Here is the result, where the color is green or the shape is rectangle:

Example of DataFrame in Python

Example 4: Select rows where the price is not equal to 15

You can use the combination of symbols != to select the rows where the price is not equal to 15:

df.loc[df.Price != 15]

from pandas import DataFrame

Boxes = {'Color': ['Green','Green','Green','Blue','Blue','Red','Red','Red'],
         'Shape': ['Rectangle','Rectangle','Square','Rectangle','Square','Square','Square','Rectangle'],
         'Price': [10,15,5,5,10,15,15,5]
        }

df = DataFrame(Boxes, columns= ['Color','Shape','Price'])

not_eqaul_to = df.loc[df.Price != 15]
print (not_eqaul_to)

Once you run the code, you’ll get all the rows where the price is not equal to 15:

Pandas DataFrame

Finally, the following source provides additional information about indexing and selecting data.