Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

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

like image 641
Timeless Avatar asked Jan 21 '26 20:01

Timeless


1 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
like image 130
wjandrea Avatar answered Jan 23 '26 08:01

wjandrea



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!