根据条件在Pandas DataFrame中选择行
让我们看看如何根据Pandas DataFrame中的某些条件选择行。
使用'>', '=', '=', '<=', '!='
运算符根据特定的列值选择行。
代码1:使用基本方法从给定数据框中选择'Percentage'大于80的所有行。
# 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)
输出:
代码2:使用选择从给定数据帧中'Percentage'大于80的所有行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)
输出:
代码#3:使用选择从给定数据帧中'Percentage'不等于95的所有行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)
输出:
使用isin()
数据框的方法选择列值存在于列表中的那些行。
代码1:使用基本方法,从给定数据框中选择选项列表中存在'Stream'的所有行。
# 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)
输出:
代码2:使用选择从给定数据帧中选项列表中存在'Stream'的所有行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)
输出:
代码3:使用选择从给定数据帧中选项列表中不存在'Stream'的所有行.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)
输出:
使用'&'
运算符根据多个列条件选择行。
代码1: 使用基本方法,从给定数据框中选择'Age'等于21并且'Stream'出现在选项列表中的所有行。
# 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)
输出:
代码2:使用.loc []从给定数据框中选择所有行,其中'Age'等于21,并且'Stream'出现在选项列表中。
# 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)
输出: