Sydney
Sydney

Reputation: 3

change index name in python

I would like to reset the name of the indices in a DataFrame in python to the same name with the first number dropped.

For example if my DataFrame is:

                 Sample 1                Sample 2
    2706   retinopathy positive        retinopathy negative
    2707   retinopathy negative        retinopathy negative
    2708   retinopathy positive        retinopathy positive

I want it to become:

                Sample 1                 Sample 2
     706   retinopathy positive        retinopathy negative
     707   retinopathy negative        retinopathy negative
     708   retinopathy positive        retinopathy positive

I have tried:

ret_metadata.rename(index={2706: '706'})

However I would like to change the index name for many different rows, maybe in a loop. Does anyone know how I could do this?

Thank you so much!

Upvotes: 0

Views: 844

Answers (2)

yaccob
yaccob

Reputation: 1333

If you prefer a mathematical solution (no conversion to string) you could do something like this:

import math
# ...
df.index = df.index % (pow(10, int(math.log(df.index, 10))))

... when you always want to remove the first digit.

If you want to remove all but the last three digits it becomes much simpler:

df.index = df.index % 1000

Upvotes: 0

G. Anderson
G. Anderson

Reputation: 5955

You can use string operations as in this answer by casting it to a string, slicing it, and casting it back to int.

df

        Sample_1                Sample_2
2706    retinopathy_positive    retinopathy_negative
2707    retinopathy_negative    retinopathy_negative
2708    retinopathy_positive    retinopathy_positive

(You can skip the second cast if you don't care about the index being numeric)

df.index=df.index.astype(str).str[1:].astype(int)

df

    Sample_1                Sample_2
706 retinopathy_positive    retinopathy_negative
707 retinopathy_negative    retinopathy_negative
708 retinopathy_positive    retinopathy_positive

df.index

Int64Index([706, 707, 708], dtype='int64')

Upvotes: 1

Related Questions