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')
To check if a value exists in the Index of a Pandas DataFrame, use the in keyword on the index property.
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.
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.
This should do the trick
'g' in df.index
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With