与Series对象类似,轴索引也有一个map方法:
In [83]: df = pd.DataFrame(np.arange(12).reshape((3, 4)), ...: index=['Ohio', 'Colorado', 'New York'], ...: columns=['one', 'two', 'three', 'four']) ...: In [84]: transform = lambda x: x[:4].upper() # 截取前4个字符并大写 In [85]: df.index.map(transform) # map的结果 Out[85]: Index(['OHIO', 'COLO', 'NEW '], dtype='object') In [86]: df.index = df.index.map(transform) #用结果修改原来的index In [87]: df Out[87]: one two three four OHIO 0 1 2 3 COLO 4 5 6 7 NEW 8 9 10 11
还可以使用rename方法修改索引,且不修改原数据:
# 参数的值是对索引进行修改的处理函数,比如str.title In [88]: df.rename(index=str.title, columns=str.upper) Out[88]: ONE TWO THREE FOUR Ohio 0 1 2 3 Colo 4 5 6 7 New 8 9 10 11 In [89]: df # 原值未变 Out[89]: one two three four OHIO 0 1 2 3 COLO 4 5 6 7 NEW 8 9 10 11
或者使用字典的方式,将指定的索引重命名为新值:
In [90]: df.rename(index={'OHIO': 'INDIANA'}, ...: columns={'three': 'peekaboo'}) ...: Out[90]: one two peekaboo four INDIANA 0 1 2 3 COLO 4 5 6 7 NEW 8 9 10 11 In [91]: df Out[91]: one two three four OHIO 0 1 2 3 COLO 4 5 6 7 NEW 8 9 10 11
使用inplace=True可以原地修改数据集。