重命名轴索引

from pandas import DataFrame,Series
import pandas as pd
import numpy as np

data = DataFrame(np.arange(12).reshape((3,4)),
                 index=["Aa","Bb","Cc"],
                 columns=["one","two","three","four"])
print(data)
'''
   one  two  three  four
A    0    1      2     3
B    4    5      6     7
C    8    9     10    11
'''
# 轴标签map方法
data.index = data.index.map(str.upper)
print(data)
'''
    one  two  three  four
AA    0    1      2     3
BB    4    5      6     7
CC    8    9     10    11
'''
# 不修改原数据 rename
new_data1 = data.rename(index=str.title,columns=str.upper)
# 结合字典对部分轴标签进行更新
new_data2 = data.rename(index={"AA":"AaA"},
                        columns={"three":"Three"})
print(new_data2)
'''
     one  two  Three  four
AaA    0    1      2     3
BB     4    5      6     7
CC     8    9     10    11
'''
# 希望就地修改数据集,inplace=True
data.rename(index={"BB":"BbB"},inplace=True)
print(data)
'''
     one  two  three  four
AA     0    1      2     3
BbB    4    5      6     7
CC     8    9     10    11
'''

 

posted @ 2021-07-01 15:17  OTAKU_nicole  阅读(56)  评论(0编辑  收藏  举报