Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a value exists in pandas dataframe index

I am sure there is an obvious way to do this but cant think of anything slick right now.

Basically instead of raising exception I would like to get True or False to see if a value exists in pandas df index.

import pandas as pd df = pd.DataFrame({'test':[1,2,3,4]}, index=['a','b','c','d']) df.loc['g']  # (should give False) 

What I have working now is the following

sum(df.index == 'g') 
like image 961
Abhi Avatar asked May 08 '14 17:05

Abhi


People also ask

How do you check if a value exists in a index of DataFrame?

To check if a value exists in the Index of a Pandas DataFrame, use the in keyword on the index property.

How do you check if a value exists in pandas DataFrame?

You can check if a column contains/exists a particular value (string/int), list of multiple values in pandas DataFrame by using pd. series() , in operator, pandas. series. isin() , str.

How do you check if a value is in a series pandas?

isin() function check whether values are contained in Series. It returns a boolean Series showing whether each element in the Series matches an element in the passed sequence of values exactly.


1 Answers

This should do the trick

'g' in df.index 
like image 60
Guillaume Jacquenot Avatar answered Oct 20 '22 11:10

Guillaume Jacquenot