行列取名
data=np.random.randn(3,4)
df1=pd.DataFrame(data,index=pd.Index(['a','b','c'],name='index',),columns=pd.Index(list('qwer'),name='columns'))
print(df1)
'''
columns q w e r
index
a -0.880477 -0.092442 -1.288179 0.383557
b 1.545453 -0.578797 1.138462 -1.219502
c -0.108588 0.514683 -0.535642 -1.064221
'''
print(df1.index.name) #index
print(df1.columns.name) #columns
索引重命名
frame=pd.DataFrame(np.random.randint(1,5,size=12).reshape(3,4),
index=list('abc'),columns=list('1234'))
frame.rename(index={'a':'a1','b':'b1'},columns={'1':'a1'},inplace=True)
print(frame)
'''
a1 2 3 4
a1 2 1 4 1
b1 2 3 1 1
c 2 3 2 3
'''
#是不是感觉比reindex强大,0.0
索引重建(重新赋值)
data=np.random.randint(100,size=12).reshape(3,4)
df1=pd.DataFrame(data,index=list('abc'),columns=range(4))
df2=df1.reindex(['a','b','c','d'],axis=0)
print(df2)
'''
如果索引值之前不存在,引用缺失值
0 1 2 3
a 86.0 69.0 34.0 26.0
b 64.0 24.0 27.0 86.0
c 20.0 69.0 19.0 35.0
d NaN NaN NaN NaN
'''
重复轴索引
data=np.random.randint(100,size=12).reshape(3,4)
df1=pd.DataFrame(data,index=[0,0,1])
print(df1)
'''
0 1 2 3
0 52 29 75 78
0 94 6 24 89
1 70 75 92 96
'''
0 1 2 3
a 60 68 74 74
a 56 71 9 97
b 18 1 58 56
df1.index.is_unique ---False
df1.loc['a']
'''
0 1 2 3
a 17 95 62 14
a 74 60 35 67
'''
索引对象的方法
'''
索引对象理解为一维数组即可
'''
append:同列表+列表
difference:差集
intersect1d:交集
union:并集
delete:删除位置i的值
drop:删除元素
insert:在指定位置插入元素
is_monotonic:是否递增
is_unique:是否唯一
unique:去重