2

How can I change the value of the index in a pandas dataframe?

for example, from this:

In[1]:plasmaLipo
Out[1]: 
         Chyloquantity  ChyloSize  VLDLquantity  VLDLSize  LDLquantity  \
Sample                                                                 
1010107          1.07       87.0         115.0      38.7        661.0   
1020107          1.13       88.0         119.0      38.8        670.0   
1030107          0.66       87.0         105.0      37.7        677.0   
1040107          0.74       88.0         100.0      38.1        687.0   
...

to this:

In[1]:plasmaLipo
Out[1]: 
         Chyloquantity  ChyloSize  VLDLquantity  VLDLSize  LDLquantity  \
Sample                                                                 
1010             1.07       87.0         115.0      38.7        661.0   
1020             1.13       88.0         119.0      38.8        670.0   
1030             0.66       87.0         105.0      37.7        677.0   
1040             0.74       88.0         100.0      38.1        687.0   
...

i.e. use only the first 4 digits of the index.

I tried to convert the index in a list and then modify it:

indx = list(plasmaLipo.index.values)
newindx = []
for items in indx:
    newindx.append(indx[:3])
print newindx

but gives me back the first 3 elements of the list:

Out[2] [[1010101, 1020101, 1030101], [1010101, 1020101, 1030101], .....
1
  • IIUC then plasmaLipo.index = plasmaLipo.index.astype(str).str[:4].astype(int) should work Commented Sep 16, 2016 at 14:41

1 Answer 1

2

You can convert the index to str dtype using astype and then slice the string values, cast back again using astype and overwrite the index attribute:

In [30]:
df.index = df.index.astype(str).str[:4].astype(int)
df

Out[30]:
        Chyloquantity  ChyloSize  VLDLquantity  VLDLSize  LDLquantity
Sample                                                               
1010             1.07       87.0         115.0      38.7        661.0
1020             1.13       88.0         119.0      38.8        670.0
1030             0.66       87.0         105.0      37.7        677.0
1040             0.74       88.0         100.0      38.1        687.0

what you tried:

for items in indx:
    newindx.append(indx[:3])

looped over each index value in for items in indx but you then appended a slice of the entire index on the next line newindx.append(indx[:3]) which is why it repeated the first 3 index values for each index item

it would've worked if you did:

for items in indx:
    newindx.append(items[:3])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.