> ## Content Index
> Fetch the complete content index at: https://datascientyst.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Convert a MultiIndex to type String or List of Strings in Pandas
- URL: https://datascientyst.com/how-to-convert-a-multiindex-to-type-string-or-list-of-strings-in-pandas/
- Published: 2025-05-16T13:39:45.000Z
- Updated: 2025-05-16T13:39:45.000Z
- Author: John D K
- Tags: MultiIndex

To convert Pandas MultiIndex to list of strins or Sting we have several options:

**(1) Lambda and custom format**

```python
midx.to_series().apply(lambda x: '{0}-{1}-{1}'.format(*x)).values

```

**(2) List comprehension**

```python
array(['11-21-21', '11-22-22', '12-21-21', '12-22-22'], dtype=object)

```

## Data

|    |    |    | Grade |
| -- | -- | -- | ----- |
| 11 | 21 | 31 | A     |
| 22 | 32 | B  |       |
| 12 | 21 | 33 | A     |
| 22 | 34 | C  |       |

Sample data:

```python
import pandas as pd

df = pd.DataFrame(
    {"Grade": ["A", "B", "A", "C"]},
    index=[
        ["11", "11", "12", "12"],
        ["21", "22", "21", "22"],
        ["31", "32", "33", "34"]
    ]
)

```

## Lambda and custom format

```python
midx.to_series().apply(lambda x: '{0}-{1}-{1}'.format(*x)).values

```

result:

```
array(['11-21-21', '11-22-22', '12-21-21', '12-22-22'], dtype=object)

```

## List comprehension

```python
array(['11-21-21', '11-22-22', '12-21-21', '12-22-22'], dtype=object)

```

result:

```
['31 21 11', '32 22 11', '33 21 12', '34 22 12']

```

## Flatten MultiIndex

```python
from itertools import starmap

def flat2(midx, sep=''):
    fstr = sep.join(['{}'] * midx.nlevels)
    return pd.Index(starmap(fstr.format, midx))

flat2(midx, sep='_')   

```

Result:

```
Index(['11_21_31', '11_22_32', '12_21_33', '12_22_34'], dtype='object')

```

For more examples and details on MultiIndex flattening please check: [How to Flatten a MultiIndex in Pandas](https://datascientyst.com/flatten-multiindex-in-pandas/)

## Resource

- [How do I convert a MultiIndex to type string](https://stackoverflow.com/questions/39111347/how-do-i-convert-a-multiindex-to-type-string?ref=datascientyst.com)