Lucas Teixeira
Lucas Teixeira

Reputation: 48

Is there a way to fill the empty cell above index title in Pandas?

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!

Upvotes: 0

Views: 280

Answers (1)

MrJ
MrJ

Reputation: 376

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:])

Upvotes: 1

Related Questions