四川省建设厅网站首页,山西建设投资集团有限公司,seo优化技巧,苏州苏网建设公司在建工程文章目录一、Series 结构二、数据结构 Series 创建1. 创建1.1 列表/数组作为数据源创建 Series1.2 字典作为数据源创建 Series1.3 通过标量创建2. 参数说明2.1 index 参数2.2 name 参数2.3 copy 参数三、Series 的索引/切片1. 下标索引2. 标签索引3. 切片四、Series 数据结构的…
文章目录一、Series 结构二、数据结构 Series 创建1. 创建1.1 列表/数组作为数据源创建 Series1.2 字典作为数据源创建 Series1.3 通过标量创建2. 参数说明2.1 index 参数2.2 name 参数2.3 copy 参数三、Series 的索引/切片1. 下标索引2. 标签索引3. 切片四、Series 数据结构的基本技巧1. 查看前几条和后几条数据2. 重新索引reindex3. 对齐运算4. 删除和添加一、Series 结构
Series 结构也称 Series 序列是 Pandas 常用的数据结构之一它是一种类似于一维数组的结构由一组数据值value和一组标签组成其中标签与数据值具有对应关系。标签不必是唯一的但必须是可哈希类型也就是不可变类型就像整型、字符串型等。该对象既支持基于整数的索引也支持基于标签的索引并提供了许多方法来执行涉及索引的操作。ndarray 的统计方法已被覆盖以自动排除缺失的数据目前表示为 NaN。Series 可以保存任何数据类型比如整数、字符串、浮点数、Python 对象等它的标签默认为整数从 0 开始依次递增。Series 的结构图如下所示 通过标签我们可以更加直观地查看数据所在的索引位置。在这里我们引入 numpy 和 pandas 库便于后续的操作。
import numpy as np
import pandas as pd二、数据结构 Series 创建
Series 的语法模板如下
pd.Series(dataNone, indexNone, dtypeNone, nameNone, copyFalse)其参数含义如下data 表示输入的数据可以是列表、常量、ndarray 数组等,如果是字典,则保持参数顺序。index 表示索引值必须是可散列的也就是不可变数据类型就像 strbytes 和数值类型)并且与数据具有相同的长度允许使用非唯一索引值。如果未提供将默认为 RangeIndex012…n。dtype 表示输出系列的数据类型。如果未指定将从数据中推断数据类型。name 是给 Series 定义一个名称。copy 表示对 data 进行拷贝默认为 False仅影响 Series 和 ndarray 数组。
1. 创建
1.1 列表/数组作为数据源创建 Series
1 以列表作为数据创建 Series。
ar_list [3,10,3,4,5]
print(type(ar_list))
s1 pd.Series(ar_list)
print(s1)
print(type(s1))
#class list
#0 3
#1 10
#2 3
#3 4
#4 5
#dtype: int64
#class pandas.core.series.Series2 以数组作为数据创建 Series。
np_rand np.arange(1,6)
s1 pd.Series(np_rand)
s1
#0 1
#1 2
#2 3
#3 4
#4 5
#dtype: int323 通过 index 和 values 属性取得对应的标签和值。我们可以取出 Series 当中的所有标签值默认为 RangeIndex012…n。
s1.index
#RangeIndex(start0, stop5, step1)当然我们可以强制转化为列表输出。
list(s1.index)
#[0, 1, 2, 3, 4]我们也取出 Series 的所有数据值他们的数据类型为 ndarray。
print(s1.values, type(s1.values))
#[1 2 3 4 5] class numpy.ndarray4 通过标签取得对应的值或者修改对应的值。我们可以输出 s1 当中索引为 1 的数据。
s1[1]
#2我们也可以修改 s1 当中索引为 2 的数据。
s1[2] 50
s1
#0 1
#1 2
#2 50
#3 4
#4 5
#dtype: int32如果我们直接以负数作为索引值对 s1 进行操作会直接报错但可以通过新增索引和数据值进行操作。5 和列表索引区别。a 默认的索引 RangeIndex不能使用负值来表示从后往前找元素。
s1[-1] 20
s1
# 0 1
# 1 2
# 2 50
# 3 4
# 4 5
#-1 20
#dtype: int64b 获取不存在的索引值对应数据会报错但是可以赋值相当于新增数据。
s1[-1] 20
print(s1)
print(s1.index)
# 0 1
# 1 2
# 2 50
# 3 4
# 4 5
#-1 20
#dtype: int64
#Int64Index([0, 1, 2, 3, 4, -1], dtypeint64)c 可以新增不同类型索引的数据新增不同类型索引的数据索引的类型会发生自动变化。
s1[a] 40
s1.index
#Index([0, 1, 2, 3, 4, -1, a], dtypeobject)1.2 字典作为数据源创建 Series
1 以字典作为数据创建 Series。
d {a: 1, b: 2, c: 3}
ser pd.Series(datad)
ser
#a 1
#b 2
#c 3
#dtype: int642 通过 index 和 values 属性取得对应的标签和值。具体可见如下例子。
ser.index
ser.values
#Index([a, b, c], dtypeobject)
#array([1, 2, 3], dtypeint64)3 通过标签取得对应的值或者修改对应的值。和使用列表、数组创建 Series 一样我们可以通过调用标签得到对应的数据。
ser[a]
#1通过标签修改对应的数据。
ser[s] 50
#a 1
#b 2
#c 3
#s 50
#dtype: int64如果标签非数值型我们既可以用标签获取值也可以用标签的下标获取值。
ser[0]
#1负数表示从后往前进行索引。
ser[-1]
#50也可以直接全部进行修改。
d {a: 1, 5: 2, c: 3}
ser1 pd.Series(datad)
ser1
#a 1
#5 2
#c 3
#dtype: int64那么当标签存在数值型的数据就不可以使用标签的下标获取值不然会直接报错。4 取得数据时先进行标签的检查如果标签中没有再进行索引的检查都不存在则报错。
1.3 通过标量创建
s pd.Series(100,indexrange(5))
s
#0 100
#1 100
#2 100
#3 100
#4 100
#dtype: int642. 参数说明
2.1 index 参数
索引值必须是可散列的(不可变数据类型例如 strbytes 和数值类型)并且与数据具有相同的长度允许使用非唯一索引值。如果未提供将默认为 RangeIndex012…n。1 使用显式索引的方法定义索引标签。当我们自定义索引标签即显示索引时需要和数据长度一致。
data np.array([a,b,c,d])
s pd.Series(data,index[100,101,102,103])
s
#100 a
#101 b
#102 c
#103 d
#dtype: object2 从指定索引的字典构造序列。
d {a: 1, b: 2, c: 3}
ser pd.Series(d, index[a, b, c])
ser
#a 1
#b 2
#c 3
#dtype: int643 当传递的索引值未匹配对应的字典键时使用 NaN非数字填充。
d {a: 1, b: 2, c: 3}
ser pd.Series(datad, index[x, b, z])
ser
#x NaN
#b 2.0
#z NaN
#dtype: float64这里需要注意的是索引是首先使用字典中的键构建的。在此之后用给定的索引值对序列重新编制索引因此我们得到所有 NaN。4 通过匹配的索引值改变创建 Series 数据的顺序。
d {a: 1, b: 2, c: 3}
ser pd.Series(datad, index[c, b, a])
ser
#c 3
#b 2
#a 1
#dtype: int642.2 name 参数
我们可以给一个 Series 对象命名也可以给一个 Series 数组中的索引列起一个名字pandas 为我们设计好了对象的属性并在设置了 name 属性值用来进行名字的设定。以下程序可以用来完成该操作。
dict_data1 {Beijing:2200,Shanghai:2500,Shenzhen:1700
}
data1 pd.Series(dict_data1)
data1.name City_Data
data1.index.name City_Name
data1
#City_Name
#Beijing 2200
#Shanghai 2500
#Shenzhen 1700
#Name: City_Data, dtype: int64序列的名称如果是 DataFrame 的一部分还包括列名。如果用于形成数据帧序列的名称将成为其索引或列名。每当使用解释器显示序列时也会使用它。
2.3 copy 参数
copy 表示对 data 进行拷贝默认为 False仅影响 Series 和 ndarray 数组。我们以数组作为数据源使用数组创建 Series。
np_rand np.arange(1,6)
s1 pd.Series(np_rand)
s1
#0 1
#1 2
#2 3
#3 4
#4 5
#dtype: int32然后我们改变 Series 标签为 1 的值并在输出 Series 的对象 s1 的同时输出数组对象 np_rand。
s1[1] 50
print(s1:,s1)
print(np_rand:,np_rand)
#s1: 0 1
#1 50
#2 3
#3 4
#4 5
#dtype: int32
#np_rand: [ 1 50 3 4 5]当源数据不是 Series 和 ndarray 类型时我们以列表作为数据源使用列表创建 Series。
my_list [1,2,3,4,5,6]
s2 pd.Series(my_list)
s2
#0 1
#1 2
#2 3
#3 4
#4 5
#5 6
#dtype: int64然后我们改变 Series 标签为 1 的值并在输出 Series 的对象 s2 的同时输出数组对象 my_list。
s2[1] 50
print(s2:,s2)
print(my_list:,my_list)
#s2: 0 1
#1 50
#2 3
#3 4
#4 5
#5 6
#dtype: int64
#my_list: [1, 2, 3, 4, 5, 6]三、Series 的索引/切片
1. 下标索引
下标索引类似于列表索引。
s pd.Series(np.random.rand(5))
print(s)
print(s[3], type(s[3]), s[3].dtype)
#0 0.777657
#1 0.622071
#2 0.348129
#3 0.756216
#4 0.287849
#dtype: float64
#0.7562162366628223 class numpy.float64 float64上面的位置索引和标签索引刚好一致会使用标签索引。当使用负值时实际并不存在负数的标签索引。
2. 标签索引
当索引为 object 类型时既可以使用标签索引也可以使用位置索引。Series 类似于固定大小的 dict把 index 中的索引标签当做 key而把 Series 序列中的元素值当做 value然后通过 index 索引标签来访问或者修改元素值。使用索标签访问单个元素值。
s pd.Series(np.random.rand(5),indexlist(abcde))
print(s[b], type(s[b]), s[b].dtype)
#0.26319645172526607 class numpy.float64 float64使用索引标签访问多个元素值注意需要选择多个标签的值用 [[]] 来表示相当于 [] 中包含一个列表。
s pd.Series([6,7,8,9,10],index [a,b,c,d,e])
print(s)
print(s[[a,c,d]])
#a 6
#b 7
#c 8
#d 9
#e 10
#dtype: int64
#a 6
#c 8
#d 9
#dtype: int64多标签会创建一个新的数组。
s1 s[[b,a,e]]
s1[b] 10
print(s1:,s1)
print(s源数据:,s)
#s1: b 10
#a 6
#e 10
#dtype: int64
#s源数据: a 6
#b 7
#c 8
#d 9
#e 10
#dtype: int643. 切片
Series 使用标签切片运算与普通的 Python 切片运算不同Series 使用标签切片时其末端是包含的。Series 使用 python 切片运算即使用位置数值切片其末端是不包含。通过下标切片的方式访问 Series 序列中的数据示例如下
s pd.Series(np.random.rand(10))
s
#0 0.927452
#1 0.235768
#2 0.516178
#3 0.277643
#4 0.697771
#5 0.273533
#6 0.133503
#7 0.185826
#8 0.687192
#9 0.316528
#dtype: float64位置索引和标签索引刚好一致使用切片时如果是数值会认为是 python 切片运算不包含末端。
s pd.Series([1,2,3,4,5],index [a,b,c,d,e])
print(s)
print(s[1:4])
#a 1
#b 2
#c 3
#d 4
#e 5
#dtype: int64
#b 2
#c 3
#d 4
#dtype: int64如果想要获取最后三个元素也可以使用下面的方式
s pd.Series([1,2,3,4,5],index [a,b,c,d,e])
print(s[-3:])
#c 3
#d 4
#e 5
#dtype: int64通过标签切片的方式访问 Series 序列中的数据示例如下当 Series 使用标签切片时其末端是包含的。
s1 pd.Series([6,7,8,9,10],index [a,b,c,d,e])
s1[b:d]
#b 7
#c 8
#d 9
#dtype: int64如果首尾端一致的话就是该元素本身。
s1 pd.Series([6,7,8,9,10],index [e,d,a,b,a])
s1
#c 8
#dtype: int64在上面的索引方式我们知道了位置索引和标签索引在 index 为数值类型时候的不同。当 index 为数值类型的时候使用位置索引会抛出 keyerror 的异常也就是说当 index 为数值类型的时候索引使用的是名称索引。但是在切片的时候有很大的不同如果 index 为数值类型的时候切片使用的是位置切片。总的来说当 index 为数值类型的时候进行索引的时候相当于使用的是名称索引。进行切片的时候相当于使用的是位置切片。
四、Series 数据结构的基本技巧
1. 查看前几条和后几条数据
s pd.Series(np.random.rand(15))
s
#0 0.819404
#1 0.552555
#2 0.792454
#3 0.215595
#4 0.824303
#5 0.970804
#6 0.997465
#7 0.519955
#8 0.354990
#9 0.758266
#dtype: float64s.head() 默认查看前 5 条数据其余的看括号内的数字。
print(s.head())
print(s.head(1))
#0 0.819404
#1 0.552555
#2 0.792454
#3 0.215595
#4 0.824303
#dtype: float64
#0 0.819404
#dtype: float64s.tail() 默认查看后 5 条数据其余的看括号内的数字。
print(s.tail())
#5 0.970804
#6 0.997465
#7 0.519955
#8 0.354990
#9 0.758266
#dtype: float642. 重新索引reindex
使用可选填充逻辑使 Series 符合新索引。将 NaN 放在上一个索引中没有值的位置。除非新索引等同于当前索引并且生成新对象。当新索引在上一个索引中不存在生成新对象时对应的值设置为 NaN。
s pd.Series(np.random.rand(5),indexlist(abcde))
s1 s.reindex(list(cde))
print(s1)
print(s1)
print(s)
print(s)
#s1
#c 0.525886
#d 0.859566
#e 0.767330
#dtype: float64
#s
#a 0.148972
#b 0.934014
#c 0.525886
#d 0.859566
#e 0.767330
#dtype: float643. 对齐运算
对其运算是数据清洗的重要过程可以按索引对齐进行运算如果没对齐的位置则补 NaN最后也可以填充 NaN。
s1 pd.Series(np.random.rand(3), index[Kelly,Anne,T-C])
s2 pd.Series(np.random.rand(3), index[Anne,Kelly,LiLy])
print(s1)
print(s1)
print(s2)
print(s2)
print(s1s2)
print(s1s2)
#s1
#Kelly 0.481159
#Anne 0.066326
#T-C 0.916705
#dtype: float64
#s2
#Anne 0.090194
#Kelly 0.150472
#LiLy 0.220991
#dtype: float64
#s1s2
#Anne 0.156520
#Kelly 0.631632
#LiLy NaN
#T-C NaN
#dtype: float644. 删除和添加
s.drop() 是返回删除后的值原值不改变默认 inplaceFalse。
s pd.Series(np.random.rand(5),indexlist(abcde))
s1 s.drop(a)
print(s1)
print(s)
#b 0.918685
#c 0.613762
#d 0.142165
#e 0.309032
#dtype: float64
#a 0.630504
#b 0.918685
#c 0.613762
#d 0.142165
#e 0.309032
#dtype: float64当 inplace 参数设置为 True 时原值发生变化返回 None。
s pd.Series(np.random.rand(5),indexlist(abcde))
s1 s.drop(a,inplaceTrue)
print(s1)
print(s)
#None
#b 0.946778
#c 0.733088
#d 0.793721
#e 0.681853
#dtype: float64添加操作时如果对应的标签没有就是添加有就是修改。
s1 pd.Series(np.random.rand(5),indexlist(abcde))
print(s1)
s1[s] 100
print(s1)
#a 0.743596
#b 0.778193
#c 0.036640
#d 0.324620
#e 0.282358
#dtype: float64
#a 0.743596
#b 0.778193
#c 0.036640
#d 0.324620
#e 0.282358
#s 100.000000
#dtype: float64