Python 在数据帧中重新定义索引值

Python 在数据帧中重新定义索引值,python,pandas,Python,Pandas,关于索引数据帧,似乎还有很多其他问题,但我还没有找到一种方法来进行我想要的更改类型。如果我有一个看起来像 Value Index1 Index2 0 1 1.1 1 2 1.2 2 3 2.4 3 1 1.3 4 2 2.2 5 3 3.1 我不需要所有index1都是唯一的。我宁愿吃像这样的东西

关于索引数据帧,似乎还有很多其他问题,但我还没有找到一种方法来进行我想要的更改类型。如果我有一个看起来像

                Value
 Index1 Index2
 0      1       1.1
 1      2       1.2
 2      3       2.4
 3      1       1.3
 4      2       2.2
 5      3       3.1
我不需要所有index1都是唯一的。我宁愿吃像这样的东西

                Value
 Index1 Index2
 0      1       1.1
 0      2       1.2
 0      3       2.4
 1      1       1.3
 1      2       2.2
 1      3       3.1
有办法做到这一点吗?我认为最简单的方法是应用index1值除以3的函数,但不确定如何将函数应用于索引。也许熊猫有它自己的方法来重新定义索引值,这样的分组在考虑两个索引时仍然是唯一的

import io
import pandas as pd
text = '''\
 Index1 Index2 Value
 0      1       1.1
 1      2       1.2
 2      3       2.4
 3      1       1.3
 4      2       2.2
 5      3       3.1'''

df = pd.read_table(io.BytesIO(text), sep='\s+', index_col=[0, 1])
df.index = pd.MultiIndex.from_tuples(
    [(item[0] // 3, item[1]) for item in df.index],
    names=df.index.names)    
print(df)
屈服

               Value
Index1 Index2       
0      1         1.1
       2         1.2
       3         2.4
1      1         1.3
       2         2.2
       3         3.1
屈服

               Value
Index1 Index2       
0      1         1.1
       2         1.2
       3         2.4
1      1         1.3
       2         2.2
       3         3.1