Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

pandas index rename using dictionary

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading