Python Pandas - Get the codes (location of each label) in MultiIndex

To get the codes (location of each label) in MultiIndex, use the MultiIndex.codes property in Pandas. The codes represent the position of each label within its respective level, not the actual values.

What are MultiIndex Codes?

MultiIndex codes are integer arrays that indicate the position of each label within the sorted unique values of each level. They provide an efficient way to store hierarchical index information.

Creating a MultiIndex

First, let's create a MultiIndex using arrays ?

import pandas as pd

# Create arrays for MultiIndex
arrays = [[1, 2, 3, 4, 5], ['John', 'Tim', 'Jacob', 'Chris', 'Keiron']]

# Create MultiIndex with named levels
multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student'))

print("The Multi-index...")
print(multiIndex)
The Multi-index...
MultiIndex([(1,   'John'),
            (2,    'Tim'),
            (3,  'Jacob'),
            (4,  'Chris'),
            (5, 'Keiron')],
           names=['ranks', 'student'])

Getting the Levels and Codes

Now let's examine the levels and their corresponding codes ?

import pandas as pd

arrays = [[1, 2, 3, 4, 5], ['John', 'Tim', 'Jacob', 'Chris', 'Keiron']]
multiIndex = pd.MultiIndex.from_arrays(arrays, names=('ranks', 'student'))

# Get the levels (sorted unique values for each level)
print("The levels in Multi-index...")
print(multiIndex.levels)

# Get the location codes for each label
print("\nThe location of each label in Multi-index...")
print(multiIndex.codes)
The levels in Multi-index...
[[1, 2, 3, 4, 5], ['Chris', 'Jacob', 'John', 'Keiron', 'Tim']]

The location of each label in Multi-index...
[[0, 1, 2, 3, 4], [2, 4, 1, 0, 3]]

Understanding the Codes

The codes show the position of each original value within the sorted levels ?

  • First level codes [0, 1, 2, 3, 4]: Values [1, 2, 3, 4, 5] are already sorted
  • Second level codes [2, 4, 1, 0, 3]: 'John'?position 2, 'Tim'?position 4, 'Jacob'?position 1, etc.

Practical Example with DataFrame

Here's how codes work with actual data ?

import pandas as pd

# Create MultiIndex DataFrame
arrays = [['A', 'A', 'B', 'B'], ['X', 'Y', 'X', 'Y']]
multiIndex = pd.MultiIndex.from_arrays(arrays, names=['Group', 'Type'])
df = pd.DataFrame([10, 20, 30, 40], index=multiIndex, columns=['Value'])

print("DataFrame with MultiIndex:")
print(df)
print("\nMultiIndex codes:")
print(df.index.codes)
DataFrame with MultiIndex:
        Value
Group Type      
A     X        10
      Y        20
B     X        30
      Y        40

MultiIndex codes:
[[0, 0, 1, 1], [0, 1, 0, 1]]

Conclusion

The codes property returns integer arrays showing the position of each label within its sorted level. This internal representation allows Pandas to efficiently handle hierarchical indexing operations.

Updated on: 2026-03-26T17:21:09+05:30

364 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements