To convert Pandas MultiIndex to list of strins or Sting we have several options:
(1) Lambda and custom format
(2) List comprehension
Data
Grade | |||
---|---|---|---|
11 | 21 | 31 | A |
22 | 32 | B | |
12 | 21 | 33 | A |
22 | 34 | C |
Sample data:
Lambda and custom format
result:
array(['11-21-21', '11-22-22', '12-21-21', '12-22-22'], dtype=object)
List comprehension
result:
['31 21 11', '32 22 11', '33 21 12', '34 22 12']
Flatten MultiIndex
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