0

Current dataframe

I have created a dataframe (df) as below:

enter image description here

Expected dataframe

I want to change the index of my dataframe as below without changing the values

enter image description here

I tried setindex , reindex but not getting the desired result.

Thanks in advance

1 Answer 1

1

You can set index as shown below

>>> idx = pd.Index(['s7', 's8', 's9', 's10', 's11'])
>>> idx
Index(['s7', 's8', 's9', 's10', 's11'], dtype='object')
>>> df.set_index(idx)
     Height  Weight
s7      167     108
s8      107     180
s9      134     187
s10     147     176
s11     160     198
>>>
Sign up to request clarification or add additional context in comments.

1 Comment

df.set_index returns a new dataframe. did you assign it back to df?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.