I have several pandas data frames with different indexes. Lets say for this example I have indexes from 50 to 200, how can I check if all three given numbers for example [10, 51, 99] are within that index? (I need this for validation purposes to return errors if an index can't be called)
My idea was to use .isin() but with .any() as in the example below it only checks whether any of the values occur in the index. Is there an option to check whether ALL indexes occur? It should return True if all values occur and False as soon as one value doesn't occur.
df.index.isin([10,51,99]).any()
You can use
sum()
.isin(..)
returns abool
array, andsum()
basically returns the count ofTrue
elements.So, in your case, you should check if
sum()
is equal to the length ofvalues
you pass in:If we generalize:
Note: As mentioned by Joooeey in the below comment, this will only work if the index does not contain duplicates.