En este artículo, analicemos cómo verificar si un valor dado existe en el marco de datos o no.
Método 1: use el operador in para verificar si existe un elemento en el marco de datos.
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")
Producción :
Método 2: use el operador not in para verificar si un elemento no existe en el marco de datos.
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")
Producción :
Método 3: verifique si existe un solo elemento en el marco de datos usando el método isin () del marco de datos.
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")
Producción :
Método 4: verifique si alguno de los valores dados existe en el marco de datos usando el método isin () del marco de datos.
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")
Producción :