码迷,mamicode.com
首页 > 其他好文 > 详细

根据条件在Pandas DataFrame中选择行

时间:2020-10-26 11:54:35      阅读:30      评论:0      收藏:0      [点我收藏+]

标签:ram   using   pre   and   idt   com   image   span   编辑   

 

让我们看看如何根据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) 

输出:

技术图片

 

根据条件在Pandas DataFrame中选择行

标签:ram   using   pre   and   idt   com   image   span   编辑   

原文地址:https://www.cnblogs.com/a00ium/p/13876747.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!