Pandas的索引操作
import pandas as pd
import numpy as np
ser_obj = pd.Series(range(10))
pd_obj = pd.DataFrame(np.random.randn(3,4),columns=['a','b','c','d'])
Series和DataFrame中的索引都是Index对象
print(type(ser_obj.index))
结果为:<class 'pandas.core.indexes.range.RangeIndex'>
索引对象不可变,保证了数据的安全
df_obj.index[0] = 2
上面这句会报错的。因为索引对象不可变,保证数据的安全
index 指定行索引名
ser_obj = pd.Series(range(5),index=['a','b','c','d','e'])
行索引由0、1、2、3、4、5变为了a、b、c、d、e
行索引
ser_obj['label']
切片索引
ser_obj[2:4]
print(ser_obj['b':'d'])
结果:
b 1
c 2
dtype: int64
b 1
c 2
d 3
dtype: int64
注意,按索引名切片操作时,是包含终止索引的。
不连续索引
ser