Open In App

Selecting rows in pandas DataFrame based on conditions

Last Updated : 29 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Let’s see how to Select rows based on some conditions in Pandas DataFrame.

Selecting rows based on particular column value using '>', '=', '=', '<=', '!=' operator.

Code #1 : Selecting all the rows from the given dataframe in which ‘Percentage’ is greater than 80 using basic method.

Python3


# importing pandas
import pandas as pd

record = {

 'Name': ['Ankit', 'Amit', 'Aishwarya', 'Priyanka', 'Priya', 'Shaurya' ],
 'Age': [21, 19, 20, 18, 17, 21],
 'Stream': ['Math', 'Commerce', 'Science', 'Math', 'Math', 'Science'],
 'Percentage': [88, 92, 95, 70, 65, 78] }

# create a dataframe
dataframe = pd.DataFrame(record, columns = ['Name', 'Age', 'Stream', 'Percentage'])

print(&quot;Given Dataframe :\n&quot;, dataframe) 

# selecting rows based on condition
rslt_df = dataframe[dataframe['Percentage'] &gt; 80]

print('\nResult dataframe :\n', rslt_df)

Output :

Code #2 : Selecting all the rows from the given dataframe in which ‘Percentage’ is greater than 80 using loc.

Python3


# importing pandas
import pandas as pd

record = {
  'Name': ['Ankit', 'Amit', 'Aishwarya', 'Priyanka', 'Priya', 'Shaurya' ],
  'Age': [21, 19, 20, 18, 17, 21],
  'Stream': ['Math', 'Commerce', 'Science', 'Math', 'Math', 'Science'],
  'Percentage': [88, 92, 95, 70, 65, 78]}

# create a dataframe
dataframe = pd.DataFrame(record, columns = ['Name', 'Age', 'Stream', 'Percentage'])

print(&quot;Given Dataframe :\n&quot;, dataframe) 

# selecting rows based on condition
rslt_df = dataframe.loc[dataframe['Percentage'] &gt; 80]

print('\nResult dataframe :\n', rslt_df)

Output :

Code #3 : Selecting all the rows from the given dataframe in which ‘Percentage’ is not equal to 95 using Loc.

Python3


# importing pandas
import pandas as pd

record = {
  'Name': ['Ankit', 'Amit', 'Aishwarya', 'Priyanka', 'Priya', 'Shaurya' ],
  'Age': [21, 19, 20, 18, 17, 21],
  'Stream': ['Math', 'Commerce', 'Science', 'Math', 'Math', 'Science'],
  'Percentage': [88, 92, 95, 70, 65, 78]}

# create a dataframe
dataframe = pd.DataFrame(record, columns = ['Name', 'Age', 'Stream', 'Percentage'])

print(&quot;Given Dataframe :\n&quot;, dataframe) 

# selecting rows based on condition
rslt_df = dataframe.loc[dataframe['Percentage'] != 95]

print('\nResult dataframe :\n', rslt_df)

Output :

Selecting those rows whose column value is present in the list using isin() method of the dataframe.

Code #1 : Selecting all the rows from the given dataframe in which ‘Stream’ is present in the options list using basic method.

Python3


# importing pandas
import pandas as pd

record = {
  'Name': ['Ankit', 'Amit', 'Aishwarya', 'Priyanka', 'Priya', 'Shaurya' ],
  'Age': [21, 19, 20, 18, 17, 21],
  'Stream': ['Math', 'Commerce', 'Science', 'Math', 'Math', 'Science'],
  'Percentage': [88, 92, 95, 70, 65, 78]}

# create a dataframe
dataframe = pd.DataFrame(record, columns = ['Name', 'Age', 'Stream', 'Percentage'])

print(&quot;Given Dataframe :\n&quot;, dataframe) 

options = ['Math', 'Commerce']

# selecting rows based on condition
rslt_df = dataframe[dataframe['Stream'].isin(options)]

print('\nResult dataframe :\n', rslt_df)

Output :

Code #2 :

Selecting all the rows from the given dataframe in which ‘Stream’ is present in the options list using .loc[].

Python3


# importing pandas
import pandas as pd

record = {
  'Name': ['Ankit', 'Amit', 'Aishwarya', 'Priyanka', 'Priya', 'Shaurya' ],
  'Age': [21, 19, 20, 18, 17, 21],
  'Stream': ['Math', 'Commerce', 'Science', 'Math', 'Math', 'Science'],
  'Percentage': [88, 92, 95, 70, 65, 78]}

# create a dataframe
dataframe = pd.DataFrame(record, columns = ['Name', 'Age', 'Stream', 'Percentage'])

print(&quot;Given Dataframe :\n&quot;, dataframe) 

options = ['Math', 'Commerce']

# selecting rows based on condition
rslt_df = dataframe.loc[dataframe['Stream'].isin(options)]

print('\nResult dataframe :\n', rslt_df)

Output :

Code #3 : Selecting all the rows from the given dataframe in which ‘Stream’ is not present in the options list using .loc[].

Python3


# importing pandas
import pandas as pd

record = {
  'Name': ['Ankit', 'Amit', 'Aishwarya', 'Priyanka', 'Priya', 'Shaurya' ],
  'Age': [21, 19, 20, 18, 17, 21],
  'Stream': ['Math', 'Commerce', 'Science', 'Math', 'Math', 'Science'],
  'Percentage': [88, 92, 95, 70, 65, 78]}

# create a dataframe
dataframe = pd.DataFrame(record, columns = ['Name', 'Age', 'Stream', 'Percentage'])

print(&quot;Given Dataframe :\n&quot;, dataframe) 

options = ['Math', 'Science']

# selecting rows based on condition
rslt_df = dataframe.loc[~dataframe['Stream'].isin(options)]

print('\nresult dataframe :\n', rslt_df)

Output :

Selecting rows based on multiple column conditions using '&' operator.

Code #1 : Selecting all the rows from the given dataframe in which ‘Age’ is equal to 21 and ‘Stream’ is present in the options list using basic method.

Python3


# importing pandas
import pandas as pd

record = {
  'Name': ['Ankit', 'Amit', 'Aishwarya', 'Priyanka', 'Priya', 'Shaurya' ],
  'Age': [21, 19, 20, 18, 17, 21],
  'Stream': ['Math', 'Commerce', 'Science', 'Math', 'Math', 'Science'],
  'Percentage': [88, 92, 95, 70, 65, 78]}

# create a dataframe
dataframe = pd.DataFrame(record, columns = ['Name', 'Age', 'Stream', 'Percentage'])

print(&quot;Given Dataframe :\n&quot;, dataframe) 

options = ['Math', 'Science']

# selecting rows based on condition
rslt_df = dataframe[(dataframe['Age'] == 21) &amp;
          dataframe['Stream'].isin(options)]

print('\nResult dataframe :\n', rslt_df)

Output :

Code #2 :

Selecting all the rows from the given dataframe in which ‘Age’ is equal to 21 and ‘Stream’ is present in the options list using

.loc[].

Python3


# importing pandas
import pandas as pd

record = {
  'Name': ['Ankit', 'Amit', 'Aishwarya', 'Priyanka', 'Priya', 'Shaurya' ],
  'Age': [21, 19, 20, 18, 17, 21],
  'Stream': ['Math', 'Commerce', 'Science', 'Math', 'Math', 'Science'],
  'Percentage': [88, 92, 95, 70, 65, 78]}

# create a dataframe
dataframe = pd.DataFrame(record, columns = ['Name', 'Age', 'Stream', 'Percentage'])

print(&quot;Given Dataframe :\n&quot;, dataframe) 

options = ['Math', 'Science']

# selecting rows based on condition
rslt_df = dataframe.loc[(dataframe['Age'] == 21) &amp;
              dataframe['Stream'].isin(options)]

print('\nResult dataframe :\n', rslt_df)

Output :



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads

Delete rows in PySpark dataframe based on multiple conditions
Sort rows or columns in Pandas Dataframe based on values
How to Filter DataFrame Rows Based on the Date in Pandas?
How to Filter DataFrame Rows Based on the Date in Pandas?
How to remove rows from a Numpy array based on multiple conditions ?
How to Drop rows in DataFrame by conditions on column values?
Pyspark - Filter dataframe based on multiple conditions
Filter Pandas Dataframe with multiple conditions
SQLAlchemy Core - Selecting Rows
How to select rows from a dataframe based on column values ?
Practice Tags :