Home > Software engineering >  Is there a way to fill the empty cell above index title in Pandas?
Is there a way to fill the empty cell above index title in Pandas?

Time:06-29

I know that I can use df.index.name() to put a title on my index and it will return the index name in a new row, something like:

        column1    column2
index
  0       foo        bar

And I already did it, but I still need to save the timestamp on that df, and for organization purposes (it will be saved periodically in an excel file), I would like to put it in the "apparently empty" cell above the index, something like:

timestamp    column1    column2
  index
    0          foo        bar

Is there a way to make it work? I'm looking through pandas docs but so far I found nothing... Thanks in advance!

CodePudding user response:

No, there is no way in pandas. But what's wrong with df.index.name = 'timestamp'?, giving you

          column1 column2
timestamp
0             foo     bar

If it's just about how the printed data frame looks like, you could do the following:

df.index.name = 'index'
print('timestamp', repr(df)[6:])
  • Related