How to increment by one the colum names (header) of a dataframe

Question:

I have this kind of dataframe

     0    1    2
0  aaa  ddd  ggg
1  bbb  eee  hhh
2  ccc  fff  iii

And I’m trying to have this :

     1    2    3
0  aaa  ddd  ggg
1  bbb  eee  hhh
2  ccc  fff  iii

With pandas.DataFrame.add_prefix, unfortunately, I’m not getting the expected output :

print(df.add_prefix(+1))
    10   11   12
0  aaa  ddd  ggg
1  bbb  eee  hhh
2  ccc  fff  iii

My question might be silly but do you know how to do that with pandas, please ?

Here is the initial dataframe used :

df = pd.DataFrame({0: ['aaa', 'bbb', 'ccc'], 1: ['ddd', 'eee', 'fff'], 2: ['ggg', 'hhh', 'iii']})

A small detail : The real dataset has hundreds of columns named (0, 1, 2, ….)

Asked By: M92_

||

Answers:

You can simply increment by one. An Index behaves like a Series in this respect.

df.columns += 1

Result:

     1    2    3
0  aaa  ddd  ggg
1  bbb  eee  hhh
2  ccc  fff  iii
Answered By: wjandrea
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.