CuriosElixir
CuriosElixir

Reputation: 61

How do I rename the column number 0 above my index in pandas dataframe

I want to rename the 'source' to something else in the following dataframe

source         A to B Diff       B to C Diff
risk_qty       4.909391e+07      0.0000
risk_delta     4.900294e+07      962.5669
risk_mtm       0.000000e+00      0.0000
risk_gamma     0.000000e+00      0.0000
risk_rho       8.322120e+01      0.0000

When I print the column names & indices this is what I get

Indices : ['risk_qty', 'risk_delta', 'risk_mtm', 'risk_gamma']

Columns: ['A to B Diff', 'B to C Diff']

But my issue is it's neither a column name nor an index, is this even achievable?

EDIT: If it helps I created this dataframe after performing a grpupby sum on 'source' & then transposing the dataframe.

I tried Sayandip's solution & ended up with following

source           A to B Diff     B to C Diff
foo
risk_qty       4.909391e+07      0.0000
risk_delta     4.900294e+07      962.5669
risk_mtm       0.000000e+00      0.0000
risk_gamma     0.000000e+00      0.0000
risk_rho       8.322120e+01      0.0000

Upvotes: 2

Views: 779

Answers (1)

Sayandip Dutta
Sayandip Dutta

Reputation: 15872

Use df.index.name:

>>> df.index.name = 'foo'
>>> df

foo            A to B Diff       B to C Diff
risk_qty       4.909391e+07      0.0000
risk_delta     4.900294e+07      962.5669
risk_mtm       0.000000e+00      0.0000
risk_gamma     0.000000e+00      0.0000
risk_rho       8.322120e+01      0.0000

Same can be achieved using:

df.index.set_names('foo',inplace=True)

Ah, got your edit, try:

>>> df.columns.name = 'foo'

Upvotes: 4

Related Questions