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], .....
plasmaLipo.index = plasmaLipo.index.astype(str).str[:4].astype(int)should work