Check if a value exists in pandas dataframe index

Abhi picture Abhi · May 8, 2014 · Viewed 195.6k times · Source

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')

Answer

Guillaume Jacquenot picture Guillaume Jacquenot · May 8, 2014

This should do the trick

'g' in df.index