Pandas: Check if a value exists in single/multiple columns index dataframe
11. Check Value Existence in Index DataFrame
Write a Pandas program to check if a specified value exists in single and multiple column index dataframe.
Test Data:
0 s001 V Alberto Franco 15/05/2002 35 street1 t1 1 s002 V Gino Mcneill 17/05/2002 32 street2 t2 2 s003 VI Ryan Parkes 16/02/1999 33 street3 t3 3 s001 VI Eesha Hinton 25/09/1998 30 street1 t4 4 s002 V Gino Mcneill 11/05/2002 31 street2 t5 5 s004 VI David Parkes 15/09/1997 32 street4 t6
Sample Solution:
Python Code :
Sample Output:
Original DataFrame with single index: school_code class name date_of_birth weight t1 s001 V Alberto Franco 15/05/2002 35 t2 s002 V Gino Mcneill 17/05/2002 32 t3 s003 VI Ryan Parkes 16/02/1999 33 t4 s001 VI Eesha Hinton 25/09/1998 30 t5 s002 V Gino Mcneill 11/05/2002 31 t6 s004 VI David Parkes 15/09/1997 32 Check a value is exist in single column index dataframe: True False Create MultiIndex using columns 't_id', ‘school_code’ and 'class': name date_of_birth weight t_id school_code class t1 s001 V Alberto Franco 15/05/2002 35 t2 s002 V Gino Mcneill 17/05/2002 32 t3 s003 VI Ryan Parkes 16/02/1999 33 t4 s001 VI Eesha Hinton 25/09/1998 30 t5 s002 V Gino Mcneill 11/05/2002 31 t6 s004 VI David Parkes 15/09/1997 32 Check a value is exist in multiple columns index dataframe: True False False
For more Practice: Solve these Related Problems:
- Write a Pandas program to check if a specified value exists in the index of a DataFrame and output the result as a boolean.
- Write a Pandas program to test for the presence of a value in a MultiIndex and then display the level in which it is found.
- Write a Pandas program to verify if a given index label exists in a DataFrame and then perform an operation based on the check.
- Write a Pandas program to search for a particular value in a DataFrame’s index and then highlight the row if the value is present.
Go to:
Next: Write a Pandas program to construct a series using the MultiIndex levels as the column and index.
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.