Check if a value exists in pandas dataframe index

PythonPandasIpython

Python Problem Overview


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

Python Solutions


Solution 1 - Python

This should do the trick

'g' in df.index

Solution 2 - Python

Multi index works a little different from single index. Here are some methods for multi-indexed dataframe.

df = pd.DataFrame({'col1': ['a', 'b','c', 'd'], 'col2': ['X','X','Y', 'Y'], 'col3': [1, 2, 3, 4]}, columns=['col1', 'col2', 'col3'])
df = df.set_index(['col1', 'col2'])

in df.index works for the first level only when checking single index value.

'a' in df.index     # True
'X' in df.index     # False

Check df.index.levels for other levels.

'a' in df.index.levels[0] # True
'X' in df.index.levels[1] # True

Check in df.index for an index combination tuple.

('a', 'X') in df.index  # True
('a', 'Y') in df.index  # False

Solution 3 - Python

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.

g in df.<your selected field>.values
g in df.index.values

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.

Solution 4 - Python

Code below does not print boolean, but allows for dataframe subsetting by index... I understand this is likely not the most efficient way to solve the problem, but I (1) like the way this reads and (2) you can easily subset where df1 index exists in df2:

df3 = df1[df1.index.isin(df2.index)]

or where df1 index does not exist in df2...

df3 = df1[~df1.index.isin(df2.index)]

Solution 5 - Python

with DataFrame: df_data

>>> df_data
  id   name  value
0  a  ampha      1
1  b   beta      2
2  c     ce      3

I tried:

>>> getattr(df_data, 'value').isin([1]).any()
True
>>> getattr(df_data, 'value').isin(['1']).any()
True

but:

>>> 1 in getattr(df_data, 'value')
True
>>> '1' in getattr(df_data, 'value')
False

So fun :D

Solution 6 - Python

df = pandas.DataFrame({'g':[1]}, index=['isStop'])

#df.loc['g']

if 'g' in df.index:
    print("find g")
    
if 'isStop' in df.index:
    print("find a") 

Solution 7 - Python

I like to use:

if 'value' in df.index.get_level_values(0):
    print(True)

get_level_values method is good because it allows you to get the value in the indexes no matter if your index is simple or composite.

Use 0 (zero) if you have a single index in your dataframe [or you want to check the first index in multiple index levels]. Use 1 for the second index, and so on...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAbhiView Question on Stackoverflow
Solution 1 - PythonGuillaume JacquenotView Answer on Stackoverflow
Solution 2 - Pythonbroccoli2000View Answer on Stackoverflow
Solution 3 - PythonEzekiel KruglickView Answer on Stackoverflow
Solution 4 - PythonxxyjoelView Answer on Stackoverflow
Solution 5 - PythonSihcView Answer on Stackoverflow
Solution 6 - PythonGankView Answer on Stackoverflow
Solution 7 - PythonSamuel CorradiView Answer on Stackoverflow