Detect whether a dataframe has a MultiIndex
You can use isinstance
to check whether an object is a class (or its subclasses):
if isinstance(result.index, pandas.MultiIndex):
You can use nlevels
to check how many levels there are:
df.index.nlevels
df.columns.nlevels
If nlevels > 1
, your dataframe certainly has multiple indices.