Search code examples
pythonpandasmulti-index

Remove headers from Multiindex dataframe


How can I remove the headers of the following Multiindex dataframe?

                 con_df                   ...                             
  dim_2             1        2        3   ...           10      11      12
  dim_0 dim_1                             ...                             
  0     0          4.4     5.87     2.28  ...  6.35485e+06  410782  395.25
  0     1      4.56835  5.75216  2.12007  ...  6.35485e+06  410782  394.75
  0     2      4.65177  5.36424  1.81586  ...  6.35485e+06  410782  394.25
  1     0      4.83481  5.19509  1.59704  ...  6.35485e+06  410782  393.75
  1     1      5.05419  5.03307  1.42013  ...  6.35485e+06  410782  393.25
  1     2      6.0365   5.0122   1.25353  ...  6.35485e+06  410782  393.25

Desired result:

  0     0          4.4     5.87     2.28  ...  6.35485e+06  410782  395.25
  0     1      4.56835  5.75216  2.12007  ...  6.35485e+06  410782  394.75
  0     2      4.65177  5.36424  1.81586  ...  6.35485e+06  410782  394.25
  1     0      4.83481  5.19509  1.59704  ...  6.35485e+06  410782  393.75
  1     1      5.05419  5.03307  1.42013  ...  6.35485e+06  410782  393.25
  1     2      6.0365   5.0122   1.25353  ...  6.35485e+06  410782  393.25

Thanks in advance


Solution

  • In pandas DataFrame are always columns, here if need remove first level use DataFrame.droplevel with DataFrame.rename_axis for remove index and columns names:

    df1 = df.droplevel(0, axis=1).rename_axis(index=(None, None), columns=None)
    

    If want write DataFrame to file with no header:

    df.to_csv(file, header=None)