外包做网站的要求怎么写,做网站 360,网站备案账号,网站开发的合同范本CSDN 课程推荐#xff1a;《迈向数据科学家#xff1a;带你玩转Python数据分析》#xff0c;讲师齐伟#xff0c;苏州研途教育科技有限公司CTO#xff0c;苏州大学应用统计专业硕士生指导委员会委员#xff1b;已出版《跟老齐学Python#xff1a;轻松入门》《跟老齐学Py…
CSDN 课程推荐《迈向数据科学家带你玩转Python数据分析》讲师齐伟苏州研途教育科技有限公司CTO苏州大学应用统计专业硕士生指导委员会委员已出版《跟老齐学Python轻松入门》《跟老齐学PythonDjango实战》、《跟老齐学Python数据分析》和《Python大学实用教程》畅销图书。Pandas 系列文章
Python 数据分析三剑客之 Pandas一认识 Pandas 及其 Series、DataFrame 对象Python 数据分析三剑客之 Pandas二Index 索引对象以及各种索引操作Python 数据分析三剑客之 Pandas三算术运算与缺失值的处理Python 数据分析三剑客之 Pandas四函数应用、映射、排序和层级索引Python 数据分析三剑客之 Pandas五统计计算与统计描述Python 数据分析三剑客之 Pandas六GroupBy 数据分裂、应用与合并Python 数据分析三剑客之 Pandas七合并数据集Python 数据分析三剑客之 Pandas八数据重塑、重复数据处理与数据替换Python 数据分析三剑客之 Pandas九时间序列Python 数据分析三剑客之 Pandas十数据读写 另有 NumPy、Matplotlib 系列文章已更新完毕欢迎关注
NumPy 系列文章https://itrhx.blog.csdn.net/category_9780393.htmlMatplotlib 系列文章https://itrhx.blog.csdn.net/category_9780418.html 推荐学习资料与网站博主参与部分文档翻译
NumPy 官方中文网https://www.numpy.org.cn/Pandas 官方中文网https://www.pypandas.cn/Matplotlib 官方中文网https://www.matplotlib.org.cn/NumPy、Matplotlib、Pandas 速查表https://github.com/TRHX/Python-quick-reference-table 文章目录【1】Index 索引对象【2】Pandas 一般索引【2.1】Series 索引【2.1.1】head() / tail()【2.1.2】行索引【2.1.3】切片索引【2.1.4】花式索引【2.1.5】布尔索引【2.2】DataFrame 索引【2.2.1】head() / tail()【2.2.2】列索引【2.2.3】切片索引【2.2.4】花式索引【2.2.5】布尔索引【3】索引器loc 和 iloc【3.1】loc 标签索引【3.1.1】Series.loc【3.1.2】DataFrame.loc【3.2】iloc 位置索引【3.2.1】Series.iloc【3.2.2】DataFrame.iloc【4】Pandas 重新索引这里是一段防爬虫文本请读者忽略。
本文原创首发于 CSDN作者 TRHX。
博客首页https://itrhx.blog.csdn.net/
本文链接https://itrhx.blog.csdn.net/article/details/106698307
未经授权禁止转载恶意转载后果自负尊重原创远离剽窃【1】Index 索引对象
Series 和 DataFrame 中的索引都是 Index 对象为了保证数据的安全索引对象是不可变的如果尝试更改索引就会报错常见的 Index 种类有索引Index整数索引Int64Index层级索引MultiIndex时间戳类型DatetimeIndex。
一下代码演示了 Index 索引对象和其不可变的性质 import pandas as pdobj pd.Series([1, 5, -8, 2], index[a, b, c, d])obj.index
Index([a, b, c, d], dtypeobject)type(obj.index)
class pandas.core.indexes.base.Indexobj.index[0] e
Traceback (most recent call last):File pyshell#28, line 1, in moduleobj.index[0] eFile C:\Users\...\base.py, line 3909, in __setitem__raise TypeError(Index does not support mutable operations)
TypeError: Index does not support mutable operationsindex 索引对象常用属性
官方文档https://pandas.pydata.org/docs/reference/api/pandas.Index.html
属性描述T转置arrayindex 的数组形式常见官方文档dtype返回基础数据的 dtype 对象hasnans是否有 NaN缺失值inferred_type返回一个字符串表示 index 的类型is_monotonic判断 index 是否是递增的is_monotonic_decreasing判断 index 是否单调递减is_monotonic_increasing判断 index 是否单调递增is_uniqueindex 是否没有重复值nbytes返回 index 中的字节数ndimindex 的维度nlevelsNumber of levels.shape返回一个元组表示 index 的形状sizeindex 的大小values返回 index 中的值 / 数组import pandas as pdobj pd.Series([1, 5, -8, 2], index[a, b, c, d])obj.index
Index([a, b, c, d], dtypeobject)obj.index.array
PandasArray
[a, b, c, d]
Length: 4, dtype: objectobj.index.dtype
dtype(O)obj.index.hasnans
Falseobj.index.inferred_type
stringobj.index.is_monotonic
Trueobj.index.is_monotonic_decreasing
Falseobj.index.is_monotonic_increasing
Trueobj.index.is_unique
Trueobj.index.nbytes
16obj.index.ndim
1obj.index.nlevels
1obj.index.shape
(4,)obj.index.size
4obj.index.values
array([a, b, c, d], dtypeobject)index 索引对象常用方法
官方文档https://pandas.pydata.org/docs/reference/api/pandas.Index.html
方法描述all(self, *args, **kwargs)判断所有元素是否为真有 0 会被视为 Falseany(self, *args, **kwargs)判断是否至少有一个元素为真均为 0 会被视为 Falseappend(self, other)连接另一个 index产生一个新的 indexargmax(self[, axis, skipna])返回 index 中最大值的索引值argmin(self[, axis, skipna])返回 index 中最小值的索引值argsort(self, *args, **kwargs)对 index 从小到大排序返回排序后的元素在原 index 中的索引值delete(self, loc)删除指定索引位置的元素返回删除后的新 indexdifference(self, other[, sort])在第一个 index 中删除第二个 index 中的元素即差集drop(self, labels[, errors])在原 index 中删除传入的值drop_duplicates(self[, keep])删除重复值keep 参数可选值如下‘first’保留第一次出现的重复项‘last’保留最后一次出现的重复项False不保留重复项duplicated(self[, keep])判断是否为重复值keep 参数可选值如下‘first’第一次重复的为 False其他为 True‘last’最后一次重复的为 False其他为 TrueFalse所有重复的均为 Truedropna(self[, how])删除缺失值即 NaNfillna(self[, value, downcast])用指定值填充缺失值即 NaNequals(self, other)判断两个 index 是否相同insert(self, loc, item)将元素插入到指定索引处返回新的 indexintersection(self, other[, sort])返回两个 index 的交集isna(self)检测 index 元素是否为缺失值即 NaNisnull(self)检测 index 元素是否为缺失值即 NaNmax(self[, axis, skipna])返回 index 的最大值min(self[, axis, skipna])返回 index 的最小值union(self, other[, sort])返回两个 index 的并集unique(self[, level])返回 index 中的唯一值相当于去除重复值
all(self, *args, **kwargs) 【官方文档】 import pandas as pdpd.Index([1, 2, 3]).all()
Truepd.Index([0, 1, 2]).all()
Falseany(self, *args, **kwargs) 【官方文档】 import pandas as pdpd.Index([0, 0, 1]).any()
Truepd.Index([0, 0, 0]).any()
Falseappend(self, other) 【官方文档】 import pandas as pdpd.Index([a, b, c]).append(pd.Index([1, 2, 3]))
Index([a, b, c, 1, 2, 3], dtypeobject)argmax(self[, axis, skipna]) 【官方文档】 import pandas as pdpd.Index([5, 2, 3, 9, 1]).argmax()
3argmin(self[, axis, skipna]) 【官方文档】 import pandas as pdpd.Index([5, 2, 3, 9, 1]).argmin()
4argsort(self, *args, **kwargs) 【官方文档】 import pandas as pdpd.Index([5, 2, 3, 9, 1]).argsort()
array([4, 1, 2, 0, 3], dtypeint32)delete(self, loc) 【官方文档】 import pandas as pdpd.Index([5, 2, 3, 9, 1]).delete(0)
Int64Index([2, 3, 9, 1], dtypeint64)difference(self, other[, sort]) 【官方文档】 import pandas as pdidx1 pd.Index([2, 1, 3, 4])idx2 pd.Index([3, 4, 5, 6])idx1.difference(idx2)
Int64Index([1, 2], dtypeint64)idx1.difference(idx2, sortFalse)
Int64Index([2, 1], dtypeint64)drop(self, labels[, errors]) 【官方文档】 import pandas as pdpd.Index([5, 2, 3, 9, 1]).drop([2, 1])
Int64Index([5, 3, 9], dtypeint64)drop_duplicates(self[, keep]) 【官方文档】 import pandas as pdidx pd.Index([lama, cow, lama, beetle, lama, hippo])idx.drop_duplicates(keepfirst)
Index([lama, cow, beetle, hippo], dtypeobject)idx.drop_duplicates(keeplast)
Index([cow, beetle, lama, hippo], dtypeobject)idx.drop_duplicates(keepFalse)
Index([cow, beetle, hippo], dtypeobject)duplicated(self[, keep]) 【官方文档】 import pandas as pdidx pd.Index([lama, cow, lama, beetle, lama])idx.duplicated()
array([False, False, True, False, True])idx.duplicated(keepfirst)
array([False, False, True, False, True])idx.duplicated(keeplast)
array([ True, False, True, False, False])idx.duplicated(keepFalse)
array([ True, False, True, False, True])dropna(self[, how]) 【官方文档】 import numpy as npimport pandas as pdpd.Index([2, 5, np.NaN, 6, np.NaN, np.NaN]).dropna()
Float64Index([2.0, 5.0, 6.0], dtypefloat64)fillna(self[, value, downcast]) 【官方文档】 import numpy as npimport pandas as pdpd.Index([2, 5, np.NaN, 6, np.NaN, np.NaN]).fillna(5)
Float64Index([2.0, 5.0, 5.0, 6.0, 5.0, 5.0], dtypefloat64)equals(self, other) 【官方文档】 import pandas as pdidx1 pd.Index([5, 2, 3, 9, 1])idx2 pd.Index([5, 2, 3, 9, 1])idx1.equals(idx2)
Trueidx1 pd.Index([5, 2, 3, 9, 1])idx2 pd.Index([5, 2, 4, 9, 1])idx1.equals(idx2)
Falseintersection(self, other[, sort]) 【官方文档】 import pandas as pdidx1 pd.Index([1, 2, 3, 4])idx2 pd.Index([3, 4, 5, 6])idx1.intersection(idx2)
Int64Index([3, 4], dtypeint64)insert(self, loc, item) 【官方文档】 import pandas as pdpd.Index([5, 2, 3, 9, 1]).insert(2, A)
Index([5, 2, A, 3, 9, 1], dtypeobject)isna(self) 【官方文档】、isnull(self) 【官方文档】 import numpy as npimport pandas as pdpd.Index([2, 5, np.NaN, 6, np.NaN, np.NaN]).isna()
array([False, False, True, False, True, True])pd.Index([2, 5, np.NaN, 6, np.NaN, np.NaN]).isnull()
array([False, False, True, False, True, True])max(self[, axis, skipna]) 【官方文档】、min(self[, axis, skipna]) 【官方文档】 import pandas as pdpd.Index([5, 2, 3, 9, 1]).max()
9pd.Index([5, 2, 3, 9, 1]).min()
1union(self, other[, sort]) 【官方文档】 import pandas as pdidx1 pd.Index([1, 2, 3, 4])idx2 pd.Index([3, 4, 5, 6])idx1.union(idx2)
Int64Index([1, 2, 3, 4, 5, 6], dtypeint64)unique(self[, level]) 【官方文档】 import pandas as pdpd.Index([5, 1, 3, 5, 1]).unique()
Int64Index([5, 1, 3], dtypeint64)【2】Pandas 一般索引
由于在 Pandas 中由于有一些更高级的索引操作比如重新索引层级索引等因此将一般的切片索引、花式索引、布尔索引等归纳为一般索引。
【2.1】Series 索引
【2.1.1】head() / tail()
Series.head() 和 Series.tail() 方法可以获取的前五行和后五行数据如果向 head() / tail() 里面传入参数则会获取指定行 import pandas as pdimport numpy as npobj pd.Series(np.random.randn(8))obj
0 -0.643437
1 -0.365652
2 -0.966554
3 -0.036127
4 1.046095
5 -2.048362
6 -1.865551
7 1.344728
dtype: float64obj.head()
0 -0.643437
1 -0.365652
2 -0.966554
3 -0.036127
4 1.046095
dtype: float64obj.head(3)
0 -0.643437
1 -0.365652
2 -0.966554
dtype: float64obj.tail()
3 1.221221
4 -1.373496
5 1.032843
6 0.029734
7 -1.861485
dtype: float64obj.tail(3)
5 1.032843
6 0.029734
7 -1.861485
dtype: float64【2.1.2】行索引
Pandas 中可以按照位置进行索引也可以按照索引名index进行索引也可以用 Python 字典的表达式和方法来获取值 import pandas as pdobj pd.Series([1, 5, -8, 2], index[a, b, c, d])obj
a 1
b 5
c -8
d 2
dtype: int64obj[c]
-8obj[2]
-8b in obj
Trueobj.keys()
Index([a, b, c, d], dtypeobject)list(obj.items())
[(a, 1), (b, 5), (c, -8), (d, 2)]【2.1.3】切片索引
切片的方法有两种按位置切片和按索引名index切片注意按位置切片时不包含终止索引按索引名index切片时包含终止索引。 import pandas as pdobj pd.Series([1, 5, -8, 2], index[a, b, c, d])obj
a 1
b 5
c -8
d 2
dtype: int64obj[1:3]
b 5
c -8
dtype: int64obj[0:3:2]
a 1
c -8
dtype: int64obj[b:d]
b 5
c -8
d 2
dtype: int64【2.1.4】花式索引
所谓的花式索引就是间隔索引、不连续的索引传递一个由索引名index或者位置参数组成的列表来一次性获得多个元素 import pandas as pdobj pd.Series([1, 5, -8, 2], index[a, b, c, d])obj
a 1
b 5
c -8
d 2
dtype: int64obj[[0, 2]]
a 1
c -8
dtype: int64obj[[a, c, d]]
a 1
c -8
d 2
dtype: int64【2.1.5】布尔索引
可以通过一个布尔数组来索引目标数组即通过布尔运算如比较运算符来获取符合指定条件的元素的数组。 import pandas as pdobj pd.Series([1, 5, -8, 2, -3], index[a, b, c, d, e])obj
a 1
b 5
c -8
d 2
e -3
dtype: int64obj[obj 0]
a 1
b 5
d 2
dtype: int64obj 0
a True
b True
c False
d True
e False
dtype: bool【2.2】DataFrame 索引
【2.2.1】head() / tail()
和 Series 一样DataFrame.head() 和 DataFrame.tail() 方法同样可以获取 DataFrame 的前五行和后五行数据如果向 head() / tail() 里面传入参数则会获取指定行 import pandas as pdimport numpy as npobj pd.DataFrame(np.random.randn(8,4), columns [a, b, c, d])obja b c d
0 -1.399390 0.521596 -0.869613 0.506621
1 -0.748562 -0.364952 0.188399 -1.402566
2 1.378776 -1.476480 0.361635 0.451134
3 -0.206405 -1.188609 3.002599 0.563650
4 0.993289 1.133748 1.177549 -2.562286
5 -0.482157 1.069293 1.143983 -1.303079
6 -1.199154 0.220360 0.801838 -0.104533
7 -1.359816 -2.092035 2.003530 -0.151812obj.head()a b c d
0 -1.399390 0.521596 -0.869613 0.506621
1 -0.748562 -0.364952 0.188399 -1.402566
2 1.378776 -1.476480 0.361635 0.451134
3 -0.206405 -1.188609 3.002599 0.563650
4 0.993289 1.133748 1.177549 -2.562286obj.head(3)a b c d
0 -1.399390 0.521596 -0.869613 0.506621
1 -0.748562 -0.364952 0.188399 -1.402566
2 1.378776 -1.476480 0.361635 0.451134obj.tail()a b c d
3 -0.206405 -1.188609 3.002599 0.563650
4 0.993289 1.133748 1.177549 -2.562286
5 -0.482157 1.069293 1.143983 -1.303079
6 -1.199154 0.220360 0.801838 -0.104533
7 -1.359816 -2.092035 2.003530 -0.151812obj.tail(3)a b c d
5 -0.482157 1.069293 1.143983 -1.303079
6 -1.199154 0.220360 0.801838 -0.104533
7 -1.359816 -2.092035 2.003530 -0.151812【2.2.2】列索引
DataFrame 可以按照列标签columns来进行列索引 import pandas as pdimport numpy as npobj pd.DataFrame(np.random.randn(7,2), columns [a, b])obja b
0 -1.198795 0.928378
1 -2.878230 0.014650
2 2.267475 0.370952
3 0.639340 -1.301041
4 -1.953444 0.148934
5 -0.445225 0.459632
6 0.097109 -2.592833obj[a]
0 -1.198795
1 -2.878230
2 2.267475
3 0.639340
4 -1.953444
5 -0.445225
6 0.097109
Name: a, dtype: float64obj[[a]]a
0 -1.198795
1 -2.878230
2 2.267475
3 0.639340
4 -1.953444
5 -0.445225
6 0.097109type(obj[a])
class pandas.core.series.Seriestype(obj[[a]])
class pandas.core.frame.DataFrame【2.2.3】切片索引
DataFrame 中的切片索引是针对行来操作的切片的方法有两种按位置切片和按索引名index切片注意按位置切片时不包含终止索引按索引名index切片时包含终止索引。 import pandas as pdimport numpy as npdata np.random.randn(5,4)index [I1, I2, I3, I4, I5]columns [a, b, c, d]obj pd.DataFrame(data, index, columns)obja b c d
I1 0.828676 -1.663337 1.753632 1.432487
I2 0.368138 0.222166 0.902764 -1.436186
I3 2.285615 -2.415175 -1.344456 -0.502214
I4 3.224288 -0.500268 1.293596 -1.235549
I5 -0.938833 -0.804433 -0.170047 -0.566766obj[0:3]a b c d
I1 0.828676 -1.663337 1.753632 1.432487
I2 0.368138 0.222166 0.902764 -1.436186
I3 2.285615 -2.415175 -1.344456 -0.502214obj[0:4:2]a b c d
I1 -0.042168 1.437354 -1.114545 0.830790
I3 0.241506 0.018984 -0.499151 -1.190143obj[I2:I4]a b c d
I2 0.368138 0.222166 0.902764 -1.436186
I3 2.285615 -2.415175 -1.344456 -0.502214
I4 3.224288 -0.500268 1.293596 -1.235549【2.2.4】花式索引
和 Series 一样所谓的花式索引就是间隔索引、不连续的索引传递一个由列名columns组成的列表来一次性获得多列元素 import pandas as pdimport numpy as npdata np.random.randn(5,4)index [I1, I2, I3, I4, I5]columns [a, b, c, d]obj pd.DataFrame(data, index, columns)obja b c d
I1 -1.083223 -0.182874 -0.348460 -1.572120
I2 -0.205206 -0.251931 1.180131 0.847720
I3 -0.980379 0.325553 -0.847566 -0.882343
I4 -0.638228 -0.282882 -0.624997 -0.245980
I5 -0.229769 1.002930 -0.226715 -0.916591obj[[a, d]]a d
I1 -1.083223 -1.572120
I2 -0.205206 0.847720
I3 -0.980379 -0.882343
I4 -0.638228 -0.245980
I5 -0.229769 -0.916591【2.2.5】布尔索引
可以通过一个布尔数组来索引目标数组即通过布尔运算如比较运算符来获取符合指定条件的元素的数组。 import pandas as pdimport numpy as npdata np.random.randn(5,4)index [I1, I2, I3, I4, I5]columns [a, b, c, d]obj pd.DataFrame(data, index, columns)obja b c d
I1 -0.602984 -0.135716 0.999689 -0.339786
I2 0.911130 -0.092485 -0.914074 -0.279588
I3 0.849606 -0.420055 -1.240389 -0.179297
I4 0.249986 -1.250668 0.329416 -1.105774
I5 -0.743816 0.430647 -0.058126 -0.337319obj[obj 0]a b c d
I1 NaN NaN 0.999689 NaN
I2 0.911130 NaN NaN NaN
I3 0.849606 NaN NaN NaN
I4 0.249986 NaN 0.329416 NaN
I5 NaN 0.430647 NaN NaNobj 0a b c d
I1 False False True False
I2 True False False False
I3 True False False False
I4 True False True False
I5 False True False False这里是一段防爬虫文本请读者忽略。
本文原创首发于 CSDN作者 TRHX。
博客首页https://itrhx.blog.csdn.net/
本文链接https://itrhx.blog.csdn.net/article/details/106698307
未经授权禁止转载恶意转载后果自负尊重原创远离剽窃【3】索引器loc 和 iloc
loc 是标签索引、iloc 是位置索引注意在 Pandas1.0.0 之前还有 ix 方法即可按标签也可按位置索引在 Pandas1.0.0 之后已被移除。
【3.1】loc 标签索引
loc 标签索引即根据 index 和 columns 来选择数据。
【3.1.1】Series.loc
在 Series 中允许输入
单个标签例如 5 或 a注意5 是 index 的名称而不是位置索引标签列表或数组例如 [a, b, c]带有标签的切片对象例如 a:f。
官方文档https://pandas.pydata.org/docs/reference/api/pandas.Series.loc.html import pandas as npobj pd.Series([1, 5, -8, 2], index[a, b, c, d])obj
a 1
b 5
c -8
d 2
dtype: int64obj.loc[a]
1obj.loc[a:c]
a 1
b 5
c -8
dtype: int64obj.loc[[a, d]]
a 1
d 2
dtype: int64【3.1.2】DataFrame.loc
在 DataFrame 中第一个参数索引行第二个参数是索引列允许输入的格式和 Series 大同小异。
官方文档https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.loc.html import pandas as pdobj pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], index[a, b, c], columns[A, B, C])objA B C
a 1 2 3
b 4 5 6
c 7 8 9obj.loc[a]
A 1
B 2
C 3
Name: a, dtype: int64obj.loc[a:c]A B C
a 1 2 3
b 4 5 6
c 7 8 9obj.loc[[a, c]]A B C
a 1 2 3
c 7 8 9obj.loc[b, B]
5obj.loc[b, A:C]
A 4
B 5
C 6
Name: b, dtype: int64【3.2】iloc 位置索引
作用和 loc 一样不过是基于索引的编号来索引即根据 index 和 columns 的位置编号来选择数据。
【3.2.1】Series.iloc
官方文档https://pandas.pydata.org/docs/reference/api/pandas.Series.iloc.html
在 Series 中允许输入
整数例如 5整数列表或数组例如 [4, 3, 0]具有整数的切片对象例如 1:7。 import pandas as npobj pd.Series([1, 5, -8, 2], index[a, b, c, d])obj
a 1
b 5
c -8
d 2
dtype: int64obj.iloc[1]
5obj.iloc[0:2]
a 1
b 5
dtype: int64obj.iloc[[0, 1, 3]]
a 1
b 5
d 2
dtype: int64【3.2.2】DataFrame.iloc
官方文档https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.iloc.html
在 DataFrame 中第一个参数索引行第二个参数是索引列允许输入的格式和 Series 大同小异 import pandas as pdobj pd.DataFrame([[1, 2, 3], [4, 5, 6], [7, 8, 9]], index[a, b, c], columns[A, B, C])objA B C
a 1 2 3
b 4 5 6
c 7 8 9obj.iloc[1]
A 4
B 5
C 6
Name: b, dtype: int64obj.iloc[0:2]A B C
a 1 2 3
b 4 5 6obj.iloc[[0, 2]]A B C
a 1 2 3
c 7 8 9obj.iloc[1, 2]
6obj.iloc[1, 0:2]
A 4
B 5
Name: b, dtype: int64【4】Pandas 重新索引
Pandas 对象的一个重要方法是 reindex其作用是创建一个新对象它的数据符合新的索引。以 DataFrame.reindex 为例Series 类似基本语法如下
DataFrame.reindex(self, labelsNone, indexNone, columnsNone, axisNone, methodNone, copyTrue, levelNone, fill_valuenan, limitNone, toleranceNone)
部分参数描述如下完整参数解释参见官方文档
参数描述index用作索引的新序列既可以是 index 实例也可以是其他序列型的 Python 数据结构method插值填充方式取值如下None不填补空白pad / ffill将上一个有效的观测值向前传播到下一个有效的观测值backfill / bfill使用下一个有效观察值来填补空白nearest使用最近的有效观测值来填补空白。fill_value在重新索引的过程中需要引入缺失值时使用的替代值limit前向或后向填充时的最大填充量tolerance向前或向后填充时填充不准确匹配项的最大间距绝对值距离level在 Multilndex 的指定级别上匹配简单索引否则选其子集copy默认为 True无论如何都复制如果为 False则新旧相等就不复制
reindex 将会根据新索引进行重排。如果某个索引值当前不存在就引入缺失值 import pandas as pdobj pd.Series([4.5, 7.2, -5.3, 3.6], index[d, b, a, c])obj
d 4.5
b 7.2
a -5.3
c 3.6
dtype: float64obj2 obj.reindex([a, b, c, d, e])obj2
a -5.3
b 7.2
c 3.6
d 4.5
e NaN
dtype: float64对于时间序列这样的有序数据重新索引时可能需要做一些插值处理。method 选项即可达到此目的例如使用 ffill 可以实现前向值填充 import pandas as pdobj pd.Series([blue, purple, yellow], index[0, 2, 4])obj
0 blue
2 purple
4 yellow
dtype: objectobj2 obj.reindex(range(6), methodffill)obj2
0 blue
1 blue
2 purple
3 purple
4 yellow
5 yellow
dtype: object借助 DataFramereindex可以修改行索引和列。只传递一个序列时会重新索引结果的行 import pandas as pdimport numpy as npobj pd.DataFrame(np.arange(9).reshape((3, 3)), index[a, c, d], columns[Ohio, Texas, California])objOhio Texas California
a 0 1 2
c 3 4 5
d 6 7 8obj2 obj.reindex([a, b, c, d])obj2Ohio Texas California
a 0.0 1.0 2.0
b NaN NaN NaN
c 3.0 4.0 5.0
d 6.0 7.0 8.0列可以用 columns 关键字重新索引 import pandas as pdimport numpy as npobj pd.DataFrame(np.arange(9).reshape((3, 3)), index[a, c, d], columns[Ohio, Texas, California])objOhio Texas California
a 0 1 2
c 3 4 5
d 6 7 8states [Texas, Utah, California]obj.reindex(columnsstates)Texas Utah California
a 1 NaN 2
c 4 NaN 5
d 7 NaN 8这里是一段防爬虫文本请读者忽略。
本文原创首发于 CSDN作者 TRHX。
博客首页https://itrhx.blog.csdn.net/
本文链接https://itrhx.blog.csdn.net/article/details/106698307
未经授权禁止转载恶意转载后果自负尊重原创远离剽窃