📜  在 Python-Pandas 中使用 in & not in运算符检查 DataFrame 中是否存在值

📅  最后修改于: 2022-05-13 01:55:52.301000             🧑  作者: Mango

在 Python-Pandas 中使用 in & not in运算符检查 DataFrame 中是否存在值

在本文中,让我们讨论如何检查给定值是否存在于数据框中。
方法 1:使用 in运算符检查数据框中是否存在元素。

Python3
# 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")


Python3
# 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")


Python3
# 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")


Python3
# 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")


输出 :

方法 2:使用 not in运算符检查数据框中是否不存在元素。

Python3

# 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-isin-2

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

Python3

# 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-isin-4

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

Python3

# 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")

输出 :