Question:
I want to add multiple empty rows at start of my dataframe. I have tried using list but it dosen’t seem to return optimum result:Example df:
Col1 | col2 | col3 | col4 |
---|---|---|---|
One | Two | Three | four |
2 | 4 | 5 | 8 |
Desired df:
Col1 | col2 | col3 | col4 |
One | Two | Three | four |
2 | 4 | 5 | 8 |
Column names should also start from the nth row, I want to add n empty rows at the beginning of my Dataframe.
Answer:
I’m not sure why you would want to do this but I did it by splitting up the original dataframe into a dataframe with a row of the column names and a separate dataframe of the data. I then created a dataframe of nans to be the blank rows and joined the 3 together. You will need to import numpy for this.I created a variable
no_cols
to be the number of columns in the dataframe and no_empty_rows
to be how many empty rows to simplify code:df
back together and reset their index, then append that to df_empty_rows
:If you have better answer, please add a comment about this, thank you!