EDIT: Essentially I need a method compatible with single level column indexes and MultiIndex columns which will return the integer number of levels in the columns.
I am writing multiple DataFrames out to Excel on a single sheet, and would like to move the start_row
on to avoid overwriting any data.
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10, 4), columns=[list('ABCD'), list('EFGH')])
print(len(df)
len(df)
will return 10; how can I also get the index levels included, so that the return value is 12?
len(df.columns.levels)
will only work if the columns are a multi-index. This applies for some of my dataframes but not all. They are a mix of single level and multiindex.
Is a try except
block the best approach (catching AttributeError for the single level dataframes), or is there a more elegant way?