user2384052
user2384052

Reputation: 147

How to change the index of a dataframe in Pandas

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

Upvotes: 0

Views: 108

Answers (1)

abhilb
abhilb

Reputation: 5757

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
>>>

Upvotes: 1

Related Questions