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

Time:09-10

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, ....)

CodePudding user response:

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
  • Related