# Series 数据结构 # Series 是带有标签的一维数组,可以保存任何数据类型(整数,字符串,浮点数,Python对象等),轴标签统称为索引 import numpy as np import pandas as pd # 导入numpy、pandas模块 s = pd.Series(np.random.rand(5)) print(s) print(type(s)) # 查看数据、数据类型 print(s.index,type(s.index)) print(s.values,type(s.values)) # .index查看series索引,类型为rangeindex # .values查看series值,类型是ndarray # 核心:series相比于ndarray,是一个自带索引index的数组 → 一维数组 + 对应索引 # 所以当只看series的值的时候,就是一个ndarray # series和ndarray较相似,索引切片功能差别不大 # series和dict相比,series更像一个有顺序的字典(dict本身不存在顺序),其索引原理与字典相似(一个用key,一个用index)
输出:
0 0.229773 1 0.357622 2 0.546116 3 0.734517 4 0.686645 dtype: float64 <class 'pandas.core.series.Series'> RangeIndex(start=0, stop=5, step=1) <class 'pandas.indexes.range.RangeIndex'> [ 0.22977307 0.35762236 0.54611623 0.73451707 0.68664496] <class 'numpy.ndarray'>
# Series 创建方法一:由字典创建,字典的key就是index,values就是values dic = {'a':1 ,'b':2 , 'c':3, '4':4, '5':5} s = pd.Series(dic) print(s) # 注意:key肯定是字符串,假如values类型不止一个会怎么样? → dic = {'a':1 ,'b':'hello' , 'c':3, '4':4, '5':5}
输出:
4 4 5 5 a 1 b 2 c 3 dtype: int64
# Series 创建方法二:由数组创建(一维数组) arr = np.random.randn(5) s = pd.Series(arr) print(arr) print(s) # 默认index是从0开始,步长为1的数字 s = pd.Series(arr, index = ['a','b','c','d','e'],dtype = np.object) print(s) # index参数:设置index,长度保持一致 # dtype参数:设置数值类型
输出:
[ 0.11206121 0.1324684 0.59930544 0.34707543 -0.15652941] 0 0.112061 1 0.132468 2 0.599305 3 0.347075 4 -0.156529 dtype: float64 a 0.112061 b 0.132468 c 0.599305 d 0.347075 e -0.156529 dtype: object
# Series 创建方法三:由标量创建 s = pd.Series(10, index = range(4)) print(s) # 如果data是标量值,则必须提供索引。该值会重复,来匹配索引的长度
输出:
0 10 1 10 2 10 3 10 dtype: int64
# Series 名称属性:name s1 = pd.Series(np.random.randn(5)) print(s1) print('-----') s2 = pd.Series(np.random.randn(5),name = 'test') print(s2) print(s1.name, s2.name,type(s2.name)) # name为Series的一个参数,创建一个数组的 名称 # .name方法:输出数组的名称,输出格式为str,如果没用定义输出名称,输出为None s3 = s2.rename('hehehe') print(s3) print(s3.name, s2.name) # .rename()重命名一个数组的名称,并且新指向一个数组,原数组不变
输出:
0 -0.403084 1 1.369383 2 1.134319 3 -0.635050 4 1.680211 dtype: float64 ----- 0 -0.120014 1 1.967648 2 1.142626 3 0.234079 4 0.761357 Name: test, dtype: float64 None test <class 'str'> 0 -0.120014 1 1.967648 2 1.142626 3 0.234079 4 0.761357 Name: hehehe, dtype: float64 hehehe test
# 位置下标,类似序列 s = pd.Series(np.random.rand(5)) print(s) print(s[0],type(s[0]),s[0].dtype) print(float(s[0]),type(float(s[0]))) #print(s[-1]) # 位置下标从0开始 # 输出结果为numpy.float格式, # 可以通过float()函数转换为python float格式 # numpy.float与float占用字节不同 # s[-1]结果如何?
输出:
0 0.924575 1 0.988654 2 0.426333 3 0.216504 4 0.453570 dtype: float64 0.924575004833 <class 'numpy.float64'> float64 0.9245750048328816 <class 'float'>
# 标签索引 s = pd.Series(np.random.rand(5), index = ['a','b','c','d','e']) print(s) print(s['a'],type(s['a']),s['a'].dtype) # 方法类似下标索引,用[]表示,内写上index,注意index是字符串 sci = s[['a','b','e']] print(sci,type(sci)) # 如果需要选择多个标签的值,用[[]]来表示(相当于[]中包含一个列表) # 多标签索引结果是新的数组
输出:
a 0.714630 b 0.213957 c 0.172188 d 0.972158 e 0.875175 dtype: float64 0.714630383451 <class 'numpy.float64'> float64 a 0.714630 b 0.213957 e 0.875175 dtype: float64 <class 'pandas.core.series.Series'>
# 切片索引 s1 = pd.Series(np.random.rand(5)) s2 = pd.Series(np.random.rand(5), index = ['a','b','c','d','e']) print(s1[1:4],s1[4]) print(s2['a':'c'],s2['c']) print(s2[0:3],s2[3]) print('-----') # 注意:用index做切片是末端包含 print(s2[:-1]) print(s2[::2]) # 下标索引做切片,和list写法一样
输出:
1 0.865967 2 0.114500 3 0.369301 dtype: float64 0.411702342342 a 0.717378 b 0.642561 c 0.391091 dtype: float64 0.39109096261 a 0.717378 b 0.642561 c 0.391091 dtype: float64 0.998978363818 ----- a 0.717378 b 0.642561 c 0.391091 d 0.998978 dtype: float64 a 0.717378 c 0.391091 e 0.957639 dtype: float64
# 布尔型索引 s = pd.Series(np.random.rand(3)*100) s[4] = None # 添加一个空值 print(s) bs1 = s > 50 bs2 = s.isnull() bs3 = s.notnull() print(bs1, type(bs1), bs1.dtype) print(bs2, type(bs2), bs2.dtype) print(bs3, type(bs3), bs3.dtype) print('-----') # 数组做判断之后,返回的是一个由布尔值组成的新的数组 # .isnull() / .notnull() 判断是否为空值 (None代表空值,NaN代表有问题的数值,两个都会识别为空值) print(s[s > 50]) print(s[bs3]) # 布尔型索引方法:用[判断条件]表示,其中判断条件可以是 一个语句,或者是 一个布尔型数组!
输出:
0 2.03802 1 40.3989 2 25.2001 4 None dtype: object 0 False 1 False 2 False 4 False dtype: bool <class 'pandas.core.series.Series'> bool 0 False 1 False 2 False 4 True dtype: bool <class 'pandas.core.series.Series'> bool 0 True 1 True 2 True 4 False dtype: bool <class 'pandas.core.series.Series'> bool ----- Series([], dtype: object) 0 2.03802 1 40.3989 2 25.2001 dtype: object