Home > Back-end >  Adding a row above header columns in python pandas dataframe, without any file read/write
Adding a row above header columns in python pandas dataframe, without any file read/write

Time:08-13

I need to add a row above the header columns in a dataframe, which will be converted to an excel file, with the limitation that there cannot be any reading / writing of files locally. Because of this, I am unable to use open('filename.xls', 'w') as f:

This is because the script is to be run in a place where files cannot be read/written locally.

So for example, I want something like this

text here
*animal* *no_of_legs* *name*
cat       4            meow
bird      2            chirp
rabbit    2            bun

I have an array allAnimals consisting of all the animals data. I tried allAnimals.insert(0,['text here]) then df = pd.DataFrame(allAnimals, columns=['animals', 'no_of_legs', 'name']) to convert as a dataframe. I then use df.to_excel(xxx, index=False) but I get something like this instead:

*animal* *no_of_legs* *name*
text here
cat       4            meow
bird      2            chirp
rabbit    2            bun

Alternatively, another method I tried involved creating a new dataframe storing only 'text here', and then tried to use concat but it doesn't add the data horizontally. It adds a new column instead. The same goes for append. So this would be what I get:

            *animal* *no_of_legs* *name*
text here
             cat       4            meow
             bird      2            chirp
             rabbit    2            bun

I have read some other questions similar to this but they are not very applicable to my case, as such, I am unable to solve this. Any tips would be appreciated!

output

Remove the index column accordingly if needed.

This may be of help to you: excel file with formatting issues

Honestly, the only complication here is the limitation of not being able to read/write locally.

I went on to research on how to fix this, and came across some articles:

excel file with nicer format

  • Related