Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number of levels (depth) of index and columns in a Pandas DataFrame

Tags:

python

pandas

Python Pandas DataFrame can have hierarchical index (MultiIndex) or hierarchical columns.

I'm looking for a way to know number of levels (depth) of index and columns.

len(df.index.levels)

seems to only work with MultiIndex but it doesn't work with normal index.

Is there an attribute for this (which will works for MultiIndex but also simple Index) ?

df.index.depth

or

df.columns.depth

will be great.

One example of MultiIndex columns and index:

import pandas as pd
import numpy as np

def mklbl(prefix,n):
    return ["%s%s" % (prefix,i)  for i in range(n)]

def mi_sample():
    miindex = pd.MultiIndex.from_product([mklbl('A',4),
                                       mklbl('B',2),
                                       mklbl('C',4),
                                       mklbl('D',2)])


    micolumns = pd.MultiIndex.from_tuples([('a','foo'),('a','bar'),
                                        ('b','foo'),('b','bah')],
                                         names=['lvl0', 'lvl1'])

    dfmi = pd.DataFrame(np.arange(len(miindex)*len(micolumns)).reshape((len(miindex),len(micolumns))),
                 index=miindex,
                 columns=micolumns).sortlevel().sortlevel(axis=1)
    return(dfmi)

df = mi_sample()

So df looks like:

lvl0           a         b
lvl1         bar  foo  bah  foo
A0 B0 C0 D0    1    0    3    2
         D1    5    4    7    6
      C1 D0    9    8   11   10
         D1   13   12   15   14
      C2 D0   17   16   19   18
         D1   21   20   23   22
      C3 D0   25   24   27   26
         D1   29   28   31   30
   B1 C0 D0   33   32   35   34
         D1   37   36   39   38
      C1 D0   41   40   43   42
         D1   45   44   47   46
      C2 D0   49   48   51   50
         D1   53   52   55   54
      C3 D0   57   56   59   58
         D1   61   60   63   62
A1 B0 C0 D0   65   64   67   66
         D1   69   68   71   70
      C1 D0   73   72   75   74
         D1   77   76   79   78
      C2 D0   81   80   83   82
         D1   85   84   87   86
      C3 D0   89   88   91   90
         D1   93   92   95   94
   B1 C0 D0   97   96   99   98
         D1  101  100  103  102
      C1 D0  105  104  107  106
         D1  109  108  111  110
      C2 D0  113  112  115  114
         D1  117  116  119  118
...          ...  ...  ...  ...
A2 B0 C1 D0  137  136  139  138
         D1  141  140  143  142
      C2 D0  145  144  147  146
         D1  149  148  151  150
      C3 D0  153  152  155  154
         D1  157  156  159  158
   B1 C0 D0  161  160  163  162
         D1  165  164  167  166
      C1 D0  169  168  171  170
         D1  173  172  175  174
      C2 D0  177  176  179  178
         D1  181  180  183  182
      C3 D0  185  184  187  186
         D1  189  188  191  190
A3 B0 C0 D0  193  192  195  194
         D1  197  196  199  198
      C1 D0  201  200  203  202
         D1  205  204  207  206
      C2 D0  209  208  211  210
         D1  213  212  215  214
      C3 D0  217  216  219  218
         D1  221  220  223  222
   B1 C0 D0  225  224  227  226
         D1  229  228  231  230
      C1 D0  233  232  235  234
         D1  237  236  239  238
      C2 D0  241  240  243  242
         D1  245  244  247  246
      C3 D0  249  248  251  250
         D1  253  252  255  254

[64 rows x 4 columns]
like image 498
scls Avatar asked Oct 22 '25 03:10

scls


1 Answers

To make summarized version of comments above:

You can use .nlevels attribute which gives the number of levels for an index and columns:

df = pd.DataFrame(np.random.rand(2,2), index=[['A','A'],['B','C']], columns=['a','b'])
df
     a      b
A B  0.558  0.336
  C  0.148  0.436    

df.index.nlevels
2

df.columns.nlevels
1

As @joris mentioned above len(df.columns.levels) will not work in the example above as columns is not MultiIndex, giving:

AttributeError: 'Index' object has no attribute 'levels'

But it will work fine for index in the example above:

len(df.index.levels)
2
like image 85
Primer Avatar answered Oct 25 '25 13:10

Primer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!