0

I'm given a dataframe, to which I wish to add a title on the indexing column. I've done this:

test_set = pd.read_csv('test_set.csv')
df= pd.DataFrame(data = test_set)
df.to_csv('test_set2.csv', encoding='utf-8')
df.index.rename('indexTitle', inplace=True)

If I print this test_set now, it's got the title on the index column. But in the end the file doesn't display with the title; the change isn't saved in the file. I know I can add the title manually but would like to know if there's a way to do so through my code.

It prints this:

                                               data
indexTitle                                                   
0       data0...
1       data1...
2       data2...
...

I'm using Python 2.7, pandas 0.22

Zap
  • 325
  • 1
  • 6
  • 23
  • Possible duplicate of [Pandas index column title or name](https://stackoverflow.com/questions/18022845/pandas-index-column-title-or-name) – cs95 May 27 '18 at 21:48
  • I've seen this (alongside numerous other questions on this site) but they didn't do what I'm trying to do here (to my understanding) – Zap May 27 '18 at 21:50

1 Answers1

0

After the df.index.rename('indexTitle', inplace=True) line I added this: df.to_csv('test_set2.csv', encoding='utf-8') which updated the file I had.

Zap
  • 325
  • 1
  • 6
  • 23