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.
# 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("Given Dataframe :\n", dataframe) # selecting rows based on condition rslt_df = dataframe[dataframe['Percentage'] > 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[].
# 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("Given Dataframe :\n", dataframe) # selecting rows based on condition rslt_df = dataframe.loc[dataframe['Percentage'] > 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[].
# 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("Given Dataframe :\n", 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.
# 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("Given Dataframe :\n", 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[].
# 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("Given Dataframe :\n", 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[].
# 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("Given Dataframe :\n", 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.
# 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("Given Dataframe :\n", dataframe) options = ['Math', 'Science'] # selecting rows based on condition rslt_df = dataframe[(dataframe['Age'] == 21) & 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[].
# 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("Given Dataframe :\n", dataframe) options = ['Math', 'Science'] # selecting rows based on condition rslt_df = dataframe.loc[(dataframe['Age'] == 21) & dataframe['Stream'].isin(options)] print('\nResult dataframe :\n', rslt_df) |
Output :

Recommended Posts:
- Sort rows or columns in Pandas Dataframe based on values
- How to Drop rows in DataFrame by conditions on column values?
- Find duplicate rows in a Dataframe based on all or selected columns
- Drop rows from the dataframe based on certain condition applied on a column
- Pandas - How to shuffle a DataFrame rows
- How to Select Rows from Pandas DataFrame?
- Sorting rows in pandas DataFrame
- Ranking Rows of Pandas DataFrame
- How to iterate over rows in Pandas Dataframe
- Get all rows in a Pandas DataFrame containing given substring
- Create a list from rows in Pandas dataframe
- How to get rows/index names in Pandas dataframe
- Create a list from rows in Pandas DataFrame | Set 2
- How to Drop Rows with NaN Values in Pandas DataFrame?
- How to randomly select rows from Pandas DataFrame
- Different ways to iterate over rows in Pandas Dataframe
- Iterating over rows and columns in Pandas DataFrame
- Dealing with Rows and Columns in Pandas DataFrame
- Count the number of rows and columns of Pandas dataframe
- How to drop rows in Pandas DataFrame by index labels?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

