0

Is there a version of this but using a dictionary instead of a list? Following the example in the link what I want to do is idx.rename({'kind':'species'}).

If it matters, this is how my data frame looks like:

                                                            k_CFD (%)  k_CFD (%) error
run_name                                           CFD                                
20230104124758_TI107_HDO6034-MS_WithInterpolati... DUT      68.529412         9.576598
                                                   MCP-PMT  72.058824        21.711526
20230104124758_TI107_HDO6034-MS_WithInterpolati... DUT      77.647059        14.783042
                                                   MCP-PMT  64.411765        20.477804
20230104124758_TI107_HDO6034-MS_WithInterpolati... DUT      73.235294         6.840427
                                                   MCP-PMT  74.117647        14.379625
20230104124758_TI107_HDO6034-MS_WithInterpolati... DUT      50.294118         9.369614
                                                   MCP-PMT  64.117647        15.786002
20230104124758_TI107_HDO6034-MS_WithInterpolati... DUT      56.176471        11.285471
                                                   MCP-PMT  46.764706        15.709597
20230104124758_TI107_HDO6034-MS_WithInterpolati... DUT      62.058824        16.288946
                                                   MCP-PMT  68.823529        18.383904
20230104124758_TI107_HDO6034-MS_WithInterpolati... DUT      78.235294         8.693637
                                                   MCP-PMT  65.294118        18.947342
20230104124758_TI107_HDO6034-MS_WithInterpolati... DUT      85.588235         7.463518
                                                   MCP-PMT  81.470588        14.591893
20230104124758_TI107_HDO6034-MS_WithInterpolati... DUT      30.882353         5.703612
                                                   MCP-PMT  48.529412        15.595717
20230104124758_TI107_HDO6034-MS_WithInterpolati... DUT      62.647059        12.137801
                                                   MCP-PMT  56.470588        19.829936
20230104124758_TI107_HDO6034-MS_WithInterpolati... DUT      71.764706        11.926692
                                                   MCP-PMT  63.823529        22.158480
20230104124758_TI107_HDO6034-MS_WithInterpolati... DUT      87.058824         4.624973
                                                   MCP-PMT  71.764706        22.626157
20230104124758_TI107_HDO6034-MS_WithInterpolati... DUT      75.588235         7.859052
                                                   MCP-PMT  72.058824        14.094790

Note that I want a solution independent of the ordering of the columns in the index, that's why I want to use a dictionary and not a list. I want to rename the index column 'CFD' to something else, independently of its position.

user171780
  • 2,243
  • 1
  • 20
  • 43

2 Answers2

1

Use DataFrame.rename_axis:

df = pd.DataFrame(columns=['a'], index=pd.MultiIndex.from_product([['MALE','FEMALE'],
                              ['NAME','AGE']], 
                             names=['kind','new']))

print (df)
               a
kind   new      
MALE   NAME  NaN
       AGE   NaN
FEMALE NAME  NaN
       AGE   NaN


df = df.rename_axis(index={'kind':'species'})
print (df)
                a
species new      
MALE    NAME  NaN
        AGE   NaN
FEMALE  NAME  NaN
        AGE   NaN
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

You approach should work, make sure to assign the output or use inplace=True:

df.index = df.index.rename({'kind': 'species'})

Example input:

           0
kind type   
1    A     0
2    B     1

Output:

              0
species type   
1       A     0
2       B     1
mozway
  • 194,879
  • 13
  • 39
  • 75