重置索引reindex()
重置索引 DataFrame.reindex(labels=None,index=None,columns=None,axis=None,method=None,copy=True,fill_value=nan,limit=None)
method: 插值填充的方式,向前填充ffill,向后填充bfill
fill_values: 引入缺失值时使用的替代值
limit: 前项或者后项填充时的最大填充量
import numpy as np
import pandas as pd
arr_0 = pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])
arr_0
# ---------------------------下面是输出结果-----------------------------
a 1
b 2
c 3
d 4
e 5
dtype: int64
看下一段代码
# 重置索引不能像下面这样,重置索引是指重置原有索引
print(arr_0.reindex(['f','i','j','k','l']))
# 下面是正确的重置索引
print(arr_0.reindex(['c','b','d','e','a']))
# ---------------------------下面是输出结果------------------------------
f NaN
i NaN
j NaN
k NaN
l NaN
dtype: float64
-------------------------------------
c 3
b 2
d 4
e 5
a 1
dtype: int64
再看下一段代码
print(arr_0)
# 重置索引时多加索引
print(arr_0.reindex(['c','b','d','e','a','f','i']))
# 给这些值为nan的索引进行值的填充
print(arr_0.reindex(['c','b','d','e','a','f','i'],fill_value=8))
# ----------------------------下面是输出结果------------------------------
a 1
b 2
c 3
d 4
e 5
dtype: int64
--------------------------------------
c 3.0
b 2.0
d 4.0
e 5.0
a 1.0
f NaN
i NaN
dtype: float64
--------------------------------------
c 3
b 2
d 4
e 5
a 1
f 8
i 8
dtype: int64
重置索引的最后一段代码
print(arr_0)
# 进行向前填充,参照的是原数组的最后一个元素
print(arr_0.reindex(['c','b','d','e','a','f','i'],method='ffill'))
# 进行向后填充
print(arr_0.reindex(['c','b','d','e','a','f','i'],method='bfill'))
# --------------------------------下面是输出结果----------------------------------
a 1
b 2
c 3
d 4
e 5
dtype: int64
------------------------------------
c 3
b 2
d 4
e 5
a 1
f 5
i 5
dtype: int64
-------------------------------------
c 3.0
b 2.0
d 4.0
e 5.0
a 1.0
f NaN
i NaN
dtype: float64
从上面可看出,使用reindex时候,要用好里面的参数,method和fill_value等等。重置索引是指将原有的索引重新按位置放,而不是取新的索引名。
索引操作
arr_1 = pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])
print(arr_1)
# 对数值用下标进行索引
print(arr_1[2])
# 对数值用索引进行索引
print(arr_1['c'])
# 用位置索引进行切片
print(arr_1[2:4])
# 用索引名进行索引
print(arr_1['c':'d'])
# 通过不连续索引获得数据集
print(arr_1[[1,2,3]])
# 通过索引名来进行不连续索引
arr_1[['b','c','d']]
# ------------------------------下面是输出结果---------------------------------
a 1
b 2
c 3
d 4
e 5
dtype: int64
----------------------------------------
3
----------------------------------------
3
----------------------------------------
c 3
d 4
dtype: int64
---------------------------------------
c 3
d 4
dtype: int64
---------------------------------------
b 2
c 3
d 4
dtype: int64
---------------------------------------
b 2
c 3
d 4
dtype: int64
创建布尔类型索引对象
# 创建布尔型Series对象
arr_1_bool = arr_1>3
arr_1_bool
# 获取结果为True的数据
arr_1[arr_1_bool]
# -------------------------下面是输出结果---------------------------
a False
b False
c False
d True
e True
dtype: bool
------------------------------
d 4
e 5
dtype: int64
DataFrame的索引操作
arr_2=np.arange(12).reshape(3,4)
arr_2
# 创建DataFrame对象,并设置
# 列名和索引名。如果不进行设置列名和索引名,默认是从0开始往后递推
arr_3=pd.DataFrame(arr_2,columns=['a','b','c','d'],index=['first','second','third'])
arr_3
# 索引b列
print(arr_3['b'])
# 获取多个不连续的Series对象
print(arr_3[['b','d']])
# 使用切片取0到1行的数据对象
print(arr_3[0:2])
# 先索引0到2行的数据,再通过不连续的列索引获取b,d列的数据
print(arr_3[0:3][['a','b']])
# 获取多列数据(用例名)
print(arr_3.loc[:,['b','a']])
# 获取多列数据的第二种方法(用位置索引)
arr_3.iloc[:,[0,1]]
# ---------------------------下面是输出结果----------------------------
a b c d
first 0 1 2 3
second 4 5 6 7
third 8 9 10 11
---------------------------------------
first 1
second 5
third 9
Name: b, dtype: int32
---------------------------------------
b d
first 1 3
second 5 7
third 9 11
---------------------------------------
a b c d
first 0 1 2 3
second 4 5 6 7
--------------------------------------
a b
first 0 1
second 4 5
third 8 9
---------------------------------------
b a
first 1 0
second 5 4
third 9 8
---------------------------------------
a b
first 0 1
second 4 5
third 8 9