0

I used to_flat_index() to flatten columns and ended up with column names like ('Method', 'sum'). I am trying to remove the special characters from these. But when I try to remove them, it changes all the column names to nan

function attempted: df_pred.columns = df_pred.columns.str.replace("[(,),']", '')

Expected outcome: MethodSum

ThePyGuy
  • 17,779
  • 5
  • 18
  • 45

1 Answers1

1

It seems your columns are multi-indexed because your use to_flat_index.

>>> df
        bar                 baz                 foo                 qux
        one       two       one       two       one       two       one       two
0  0.713825  0.015553  0.036683  0.388443  0.729509  0.699883  0.125998  0.407517
1  0.820843  0.259039  0.217209  0.021479  0.845530  0.112166  0.219814  0.527205
2  0.734660  0.931206  0.651559  0.337565  0.422514  0.873403  0.979258  0.269594
3  0.314323  0.857317  0.222574  0.811631  0.313495  0.315072  0.354784  0.394564
4  0.672068  0.658103  0.402914  0.430545  0.879331  0.015605  0.086048  0.918678

Try:

>>> df.columns.to_flat_index().map(''.join)
Index(['barone', 'bartwo', 'bazone', 'baztwo',
       'fooone', 'footwo', 'quxone', 'quxtwo'],
      dtype='object')
Corralien
  • 109,409
  • 8
  • 28
  • 52