How do you select specific rows in python?

Need to select rows from Pandas DataFrame?

If so, you’ll see the full steps to select rows from Pandas DataFrame based on the conditions specified.

Step 1: Gather your data

Firstly, you’ll need to gather your data. Here is an example of a data gathered about boxes:

Color Shape Price
Green Rectangle 10
Green Rectangle 15
Green Square 5
Blue Rectangle 5
Blue Square 10
Red Square 15
Red Square 15
Red Rectangle 5

Step 2: Create a DataFrame

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

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

import pandas as pd

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 = pd.DataFrame(boxes, columns= ['Color','Shape','Price'])
print (df)

Run the code in Python and you’ll see this DataFrame:

   Color      Shape  Price
0  Green  Rectangle     10
1  Green  Rectangle     15
2  Green     Square      5
3   Blue  Rectangle      5
4   Blue     Square     10
5    Red     Square     15
6    Red     Square     15
7    Red  Rectangle      5

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:

import pandas as pd

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 = pd.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:

   Color      Shape  Price
0  Green  Rectangle     10
1  Green  Rectangle     15
2  Green     Square      5

Additional Examples of Selecting Rows from Pandas DataFrame

Let’s now review additional examples to get a better sense of selecting rows from 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:

import pandas as pd

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 = pd.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:

   Color      Shape  Price
0  Green  Rectangle     10
1  Green  Rectangle     15
4   Blue     Square     10
5    Red     Square     15
6    Red     Square     15

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:

import pandas as pd

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 = pd.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:

   Color      Shape  Price
0  Green  Rectangle     10
1  Green  Rectangle     15

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:

import pandas as pd

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 = pd.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:

   Color      Shape  Price
0  Green  Rectangle     10
1  Green  Rectangle     15
2  Green     Square      5
3   Blue  Rectangle      5
7    Red  Rectangle      5

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]

import pandas as pd

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 = pd.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:

   Color      Shape  Price
0  Green  Rectangle     10
2  Green     Square      5
3   Blue  Rectangle      5
4   Blue     Square     10
7    Red  Rectangle      5

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

How do you select a group of rows in Python?

To access a group of rows in a Pandas DataFrame, we can use the loc() method. For example, if we use df. loc[2:5], then it will select all the rows from 2 to 5.

How do I read a specific row in a DataFrame in Python?

Steps to Select Rows from Pandas DataFrame.
Step 1: Data Setup. Pandas read_csv() is an inbuilt function used to import the data from a CSV file and analyze that data in Python. ... .
Step 2: Import CSV Data. ... .
Step 3: Select Rows from Pandas DataFrame..

How do you filter certain rows in Python?

Filter Rows by Condition You can use df[df["Courses"] == 'Spark'] to filter rows by a condition in pandas DataFrame. Not that this expression returns a new DataFrame with selected rows. You can also write the above statement with a variable.

How do I select multiple rows in Python?

Select rows by multiple conditions in Pandas.
loc[] to Select mutiple rows based on column value. ... .
Select Mutiple row using loc[] and & operator. ... .
Select row based on Value in Column. ... .
Select row based on multiple values in column. ... .
Select row based on any of mutiple condition. ... .
Select Mutiple Rows using OR operator..

How do I select specific rows and columns from a DataFrame?

To select a single value from the DataFrame, you can do the following. You can use slicing to select a particular column. To select rows and columns simultaneously, you need to understand the use of comma in the square brackets.

How do I print specific rows and columns in Python?

You can use the df. loc[[2]] to print a specific row of a pandas dataframe.