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.
df = pandas.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')
Multi index works a little different from single index. Here are some methods for multi-indexed dataframe.
in df.index
works for the first level only when checking single index value.Check
df.index.levels
for other levels.Check in
df.index
for an index combination tuple.Just for reference as it was something I was looking for, you can test for presence within the values or the index by appending the ".values" method, e.g.
I find that adding the ".values" to get a simple list or ndarray out makes exist or "in" checks run more smoothly with the other python tools. Just thought I'd toss that out there for people.
This should do the trick