根据条件在Pandas DataFrame中选择行

Posted a00ium

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了根据条件在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 :
", dataframe) 

# selecting rows based on condition 
rslt_df = dataframe[dataframe[Percentage] > 80] 

print(
Result dataframe :
, 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 :
", dataframe) 

# selecting rows based on condition 
rslt_df = dataframe.loc[dataframe[Percentage] > 80] 

print(
Result dataframe :
, 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 :
", dataframe) 

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

print(
Result dataframe :
, 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 :
", dataframe) 

options = [Math, Commerce] 

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

print(
Result dataframe :
, 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 :
", dataframe) 

options = [Math, Commerce] 

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

print(
Result dataframe :
, 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 :
", dataframe) 

options = [Math, Science] 

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

print(
result dataframe :
, 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 :
", dataframe) 

options = [Math, Science] 

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

print(
Result dataframe :
, 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 :
", dataframe) 

options = [Math, Science] 

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

print(
Result dataframe :
, rslt_df) 

输出:

技术图片

 

以上是关于根据条件在Pandas DataFrame中选择行的主要内容,如果未能解决你的问题,请参考以下文章

pandas 怎么根据另一个dataframe来筛选

如何根据列值从 DataFrame 中选择行?

如何根据列值从 DataFrame 中选择行?

如何根据列值从 DataFrame 中选择行?

pandas的DataFrame的行列选择

Pandas DataFrame:根据条件替换列中的所有值