I have a csv file that is used as a pandas dataframe, now I only need to insert a dummy row before the dataframe starts like in the screenshot denoted as "Label". How can I do that? enter image description here

  • $\begingroup$ What do you want to do with the modified dataframe? What should the resulting index and column names be? Or do you want to simply create a new CSV file with the new first row? $\endgroup$
    – Lynn
    Sep 7, 2022 at 8:40

2 Answers 2


It can be done with multi-indexing (see more here):

import pandas as pd
import itertools
import numpy as np
index = np.arange(9)
headr = [('label','time'), ("","a"), ("","b")]
cols = pd.MultiIndex.from_tuples(headr)  
data = [[70 + x + y + (x * y) % 3 for x in range(3)] for y in range(9)]
df = pd.DataFrame(data, index, cols)
df.to_csv('df_multiindex.csv', index=False)

as csv

or a bit easier try this:

df = pd.read_csv('your_csv_path_goes_here')
column_list = df.columns
index_array = [['label', column_list[0]]] + [['', f'{column_list[i]}'] for i in np.arange(1,len(column_list))]
idx = pd.MultiIndex.from_tuples(index_array)
df.columns = idx

should give something like this... updated

  • $\begingroup$ is not there a more direct way? I have 255 columns, I don't want to reconstruct the whole dataframe. $\endgroup$ Sep 7, 2022 at 7:38
  • $\begingroup$ updated my answer. $\endgroup$
    – Adrian B
    Sep 8, 2022 at 15:16

I found the answer by someone in SO, but I can't find it now.

df.columns = pd.MultiIndex.from_tuples(
    zip([' ']*256, 

He posted something like this, but doing it directly, will result in an empty row below the columns names row, so I did the following and it worked for me:

    df.index.name = 'time'
    df.index +=1


    df.columns = pd.MultiIndex.from_tuples(
        zip([' ']*256, 

    df.to_csv(values["-OUTPUT_PATH-"]+'/converted.csv', index=False)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.