Selecting rows in pandas DataFrame based on conditions - GeeksforGeeks

10 Jun, 2021
Let’s see how to Select rows based on some conditions in Pandas DataFrame.
YouTubeGeeksforGeeks507K subscribersSelecting Rows in Pandas DataFrame Based on Conditions | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 9:01•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=D9FwM-76Cns" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
Code #1 : Selecting all the rows from the given dataframe in which ‘Percentage’ is greater than 80 using basic method.
# importing pandasimport 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 dataframedataframe = pd.DataFrame(record, columns = ['Name', 'Age', 'Stream', 'Percentage'])  print("Given Dataframe :\n", dataframe)   # selecting rows based on conditionrslt_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 pandasimport 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 dataframedataframe = pd.DataFrame(record, columns = ['Name', 'Age', 'Stream', 'Percentage'])  print("Given Dataframe :\n", dataframe)   # selecting rows based on conditionrslt_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 pandasimport 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 dataframedataframe = pd.DataFrame(record, columns = ['Name', 'Age', 'Stream', 'Percentage'])  print("Given Dataframe :\n", dataframe)   # selecting rows based on conditionrslt_df = dataframe.loc[dataframe['Percentage'] != 95]  print('\nResult dataframe :\n', rslt_df)
Output :
Code #1 : Selecting all the rows from the given dataframe in which ‘Stream’ is present in the options list using basic method.
# importing pandasimport 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 dataframedataframe = pd.DataFrame(record, columns = ['Name', 'Age', 'Stream', 'Percentage'])  print("Given Dataframe :\n", dataframe)   options = ['Math', 'Commerce']  # selecting rows based on conditionrslt_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 pandasimport 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 dataframedataframe = pd.DataFrame(record, columns = ['Name', 'Age', 'Stream', 'Percentage'])  print("Given Dataframe :\n", dataframe)   options = ['Math', 'Commerce']  # selecting rows based on conditionrslt_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 pandasimport 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 dataframedataframe = pd.DataFrame(record, columns = ['Name', 'Age', 'Stream', 'Percentage'])  print("Given Dataframe :\n", dataframe)   options = ['Math', 'Science']  # selecting rows based on conditionrslt_df = dataframe.loc[~dataframe['Stream'].isin(options)]  print('\nresult dataframe :\n', rslt_df)
Output :
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 pandasimport 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 dataframedataframe = pd.DataFrame(record, columns = ['Name', 'Age', 'Stream', 'Percentage'])  print("Given Dataframe :\n", dataframe)   options = ['Math', 'Science']  # selecting rows based on conditionrslt_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 pandasimport 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 dataframedataframe = pd.DataFrame(record, columns = ['Name', 'Age', 'Stream', 'Percentage'])  print("Given Dataframe :\n", dataframe)   options = ['Math', 'Science']  # selecting rows based on conditionrslt_df = dataframe.loc[(dataframe['Age'] == 21) &              dataframe['Stream'].isin(options)]  print('\nResult dataframe :\n', rslt_df)
Output :
pandas-dataframe-program
Picked
Python pandas-dataFrame
Python-pandas
Technical Scripter 2018
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
*args and **kwargs in Python
Create a Pandas DataFrame from Lists
Convert integer to string in Python
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
sum() function in Python
isupper(), islower(), lower(), upper() in Python and their applications