2

I have my df.columns = this MultiIndex

MultiIndex([(        'F', 1),
            (        'F', 2),
            (        'F', 3),
            (        'F', 4),
            (        'F', 5),
            (        'F', 6),
            (        'F', 7),
            (        'F', 8),
            (        'F', 9),
            (    'FG', 1),
            (    'FG', 2),
            (    'FG', 3),
            (    'FG', 4),
            (    'FG', 5),
            (    'FG', 6),
            (    'FG', 7),
            (    'FG', 8),
            (    'FG', 9),
            (    'R', 1),
            (    'R', 2),
            (    'R', 3),
            (    'R', 4),
            (    'R', 5),
            (    'R', 6),
            (    'R', 7),
            (    'R', 8),
            (    'R', 9),
            ('RG', 1),
            ('RG', 2),
            ('RG', 3),
            ('RG', 4),
            ('RG', 5),
            ('RG', 6),
            ('RG', 7),
            ('RG', 8),
            ('RG', 9),
            (    'FR', 1),
            (    'FR', 2),
            (    'FR', 3),
            (    'FR', 4),
            (    'FR', 5),
            (    'FR', 6),
            (    'FR', 7),
            (    'FR', 8),
            (    'FR', 9)],
           names=[None, 'Profondeur'])

I want it to become Index like this :

Index(['F_1', 'F_2', 'F_3', 'F_4', 'F_5', 'F_6', 'F_7', 'F_8', 'F_9',
       'FG_1', 'FG_2', 'FG_3', 'FG_4', 'FG_5', 'FG_6', 'FG_7', 'FG_8', 'FG_9',
       'R_1', 'R_2', 'R_3', 'R_4', 'R_5', 'R_6', 'R_7', 'R_8', 'R_9',
       'RG_1', 'RG_2', 'RG_3', 'RG_4', 'RG_5', 'RG_6', 'RG_7', 'RG_8', 'RG_9',
       'FR_1', 'FR_2', 'FR_3', 'FR_4', 'FR_5', 'FR_6', 'FR_7', 'FR_8', 'FR_9'], dtype='object')

I don't know much about MultiIndex and I obtained it after an unstack() method. In fact I want to combine both level concatenating their columns names with '_' as a separator. Also 'Profondeur' label is int and maybe it needs to be turned to str

Lemisourd
  • 135
  • 1
  • 11

1 Answers1

2

Collapse a MultiIndex into a simple Index by assigning the columns back to a list. In this case, use a list comprehension to '_'.join the two components of the MultiIndex (the values are simple tuples), with an extra step of mapping to strings because you can't string join a string with an int.

df.columns = ['_'.join(map(str, x)) for x in df.columns]

print(df.columns)
#Index(['F_1', 'F_2', 'F_3', 'F_4', 'F_5', 'F_6', 'F_7', 'F_8', 'F_9', 'FG_1',
#       'FG_2', 'FG_3', 'FG_4', 'FG_5', 'FG_6', 'FG_7', 'FG_8', 'FG_9', 'R_1',
#       'R_2', 'R_3', 'R_4', 'R_5', 'R_6', 'R_7', 'R_8', 'R_9', 'RG_1', 'RG_2',
#       'RG_3', 'RG_4', 'RG_5', 'RG_6', 'RG_7', 'RG_8', 'RG_9', 'FR_1', 'FR_2',
#       'FR_3', 'FR_4', 'FR_5', 'FR_6', 'FR_7', 'FR_8', 'FR_9'],
#      dtype='object')
ALollz
  • 57,915
  • 7
  • 66
  • 89
  • 1
    Yes, I found something like this on another question just before you answered, but there was no mapping to convert int to string. Your answer worked well ! – Lemisourd Jan 10 '22 at 17:14