I have a DataFrame with a number of index levels and a number of columns. I have a field that I know is either the name of an index level or the name of a column, but I don't know which. I wish to filter my DataFrame on this field. If the field were an index level, I would do df[df.index.get_level_values(field) == 0]
, and if it were a column I'd do df[df[field] == 0]
, but since I don't know which it is I'm stuck. Is there a way to let pandas figure out whether the field name is in the index or the columns and grab the Index/Series as appropriate?
I know that I could reset the index and then set it back, but this seems dumb and potentially problematic if not all of the index levels are named.