在Python-Pandas中使用in & not in操作符检查DataFrame中是否存在一个值

在Python-Pandas中使用in & not in操作符检查DataFrame中是否存在一个值

在这篇文章中,让我们讨论如何检查一个给定的值是否存在于数据框中。
方法1 :使用in操作符来检查一个元素是否存在于数据框架中。

# import pandas library
import pandas as pd
 
# dictionary with list object in values
details = {
    'Name' : ['Ankit', 'Aishwarya', 'Shaurya',
              'Shivangi', 'Priya', 'Swapnil'],
    'Age' : [23, 21, 22, 21, 24, 25],
    'University' : ['BHU', 'JNU', 'DU', 'BHU', 'Geu', 'Geu'],
}
 
# creating a Dataframe object
df = pd.DataFrame(details, columns = ['Name', 'Age', 'University'],
                  index = ['a', 'b', 'c', 'd', 'e', 'f'])
 
print("Dataframe: \n\n", df)
 
# check 'Ankit' exist in dataframe or not
if 'Ankit' in df.values :
    print("\nThis value exists in Dataframe")
 
else :
    print("\nThis value does not exists in Dataframe")
Python

输出 :

在Python-Pandas中使用in & not in操作符检查DataFrame中是否存在一个值

方法2:使用not in操作符来检查一个元素是否在数据框架中不存在。

# import pandas library
import pandas as pd
 
# dictionary with list object in values
details = {
    'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi', 'Priya', 'Swapnil'],
    'Age' : [23, 21, 22, 21, 24, 25],
    'University' : ['BHU', 'JNU', 'DU', 'BHU', 'Geu', 'Geu'],
}
 
# creating a Dataframe object
df = pd.DataFrame(details, columns = ['Name', 'Age', 'University'],
                  index = ['a', 'b', 'c', 'd', 'e', 'f'])
 
print("Dataframe: \n\n", df)
 
# check 'Ankit' exist in dataframe or not
if 'Ankita' not in df.values :
    print("\nThis value not exists in Dataframe")
 
else :
    print("\nThis value exists in Dataframe")
    
Python

输出 :

在Python-Pandas中使用in & not in操作符检查DataFrame中是否存在一个值

方法3 :使用数据框架的isin()方法检查数据框架中是否存在单一元素。

# import pandas library
import pandas as pd
 
# dictionary with list object in values
details = {
    'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi', 'Priya', 'Swapnil'],
    'Age' : [23, 21, 22, 21, 24, 25],
    'University' : ['BHU', 'JNU', 'DU', 'BHU', 'Geu', 'Geu'],
}
 
# creating a Dataframe object
df = pd.DataFrame(details, columns = ['Name', 'Age', 'University'],
                  index = ['a', 'b', 'c', 'd', 'e', 'f'])
 
print("Dataframe: \n\n", df)
 
# isin() methods return Boolean
# Dataframe of given Dimension
# first any() will return boolean series
# and 2nd any() will return single bool value
res = df.isin(['Ankit']).any().any()
 
if res :
    print("\nThis value exists in Dataframe")
 
else :
    print("\nThis value does not exists in Dataframe")
Python

输出 :

在Python-Pandas中使用in & not in操作符检查DataFrame中是否存在一个值

方法4 :使用dataframe的isin()方法检查Dataframe中是否存在任何给定值。

# import pandas library
import pandas as pd
 
# dictionary with list object in values
details = {
    'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi', 'Priya', 'Swapnil'],
    'Age' : [23, 21, 22, 21, 24, 25],
    'University' : ['BHU', 'JNU', 'DU', 'BHU', 'Geu', 'Geu'],
}
 
# creating a Dataframe object
df = pd.DataFrame(details, columns = ['Name', 'Age', 'University'],
                  index = ['a', 'b', 'c', 'd', 'e', 'f'])
 
print("Dataframe: \n\n", df)
 
# isin() methods return Boolean Dataframe
# of given Dimension first any() will return
# boolean series and 2nd any() will return
# single boolean value
res = df.isin(['Ankit', 'good', 30]).any().any()
 
if res :
    print("\nany of the mention value exists in Dataframe")
     
else :
    print("\nNone of thses values exists in Dataframe")
Python

输出 :

在Python-Pandas中使用in & not in操作符检查DataFrame中是否存在一个值

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册