pandas中的series数据类型详解
更新时间:2019年07月06日 11:45:29 作者:xsan
这篇文章主要介绍了pandas中的series数据类型详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
本文介绍了pandas中的series数据类型详解,分享给大家,具体如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | import pandas as pd import numpy as np import names ''' 写在前面的话: 1、series与array类型的不同之处为series有索引,而另一个没有;series中的数据必须是一维的,而array类型不一定 2、可以把series看成一个定长的有序字典,可以通过shape,index,values等得到series的属性 ''' # 1、series的创建 ''' (1)由列表或numpy数组创建 默认索引为0到N-1的整数型索引,如s1; 可以通过设置index参数指定索引,如s2; 通过这种方式创建的series,不是array的副本,即对series操作的同时也改变了原先的array数组,如s3 (2)由字典创建 字典的键名为索引,键值为值,如s4; ''' n1 = np.array([ 1 , 4 , 5 , 67 , 7 , 43 , ]) s1 = pd.Series(n1) # print(s1) ''' 1 4 5 67 7 43 dtype: int32 ''' s2 = pd.Series(n1, index = [ 'a' , 'b' , 'c' , 'd' , 'e' , 'f' ]) # print(s2) ''' a 1 b 4 c 5 d 67 e 7 f 43 dtype: int32 ''' # print(n1) ''' [ 1 4 5 67 7 43] ''' s1[ 2 ] = 100 s3 = s1 # print(s3) ''' 1 4 100 67 7 43 dtype: int32 ''' # print(n1) ''' [ 1 4 100 67 7 43] ''' dict1 = {} for i in range ( 10 , 15 ): # names.get_last_name(),随机生成英文名字 dict1[names.get_last_name()] = i s4 = pd.Series(dict1) # print(s4) ''' Poole 10 Allen 11 Davis 12 Roland 13 Brehm 14 dtype: int64 ''' # 2、series的索引 ''' (1)通过index取值,可以通过下标获取,也可以通过指定索引获取,如s6,s7 (2)通过.loc[](显示索引)获取,这种方式只能获取显示出来的索引,无法通过下标获取,如s7(推荐) (3)隐式索引,使用整数作为索引值,使用.icol[],如s9(推荐) ''' s5 = pd.Series(np.array([ 1 , 5 , 9 , 7 , 6 , 4 , 52 , 8 ]), index = [ list ( 'abcdefgh' )]) # print(s5) ''' a 1 b 5 c 9 d 7 e 6 f 4 g 52 h 8 dtype: int32 ''' s6 = s5[ 2 ] # print(s6) ''' ''' s7 = s5[ 'c' ] # print(s7) ''' c 9 dtype: int32 ''' s8 = s5.loc[ 'c' ] # print(s8) ''' c 9 dtype: int32 ''' s9 = s5.iloc[ 2 ] # print(s9) ''' ''' # 3、series的切片 ''' 1、series的切片和列表的用法类似,不同之处在于建议使用.loc[:]和.iloc[:],如s10和s11。当然直接使用[:]也可以。 2、当遇到特别长的series,我们支取出前5条或后5条数据时可以直接使用.head()或.tail() ''' s5 = pd.Series(np.array([ 1 , 5 , 9 , 7 , 6 , 4 , 52 , 8 ]), index = [ list ( 'abcdefgh' )]) # print(s5) ''' a 1 b 5 c 9 d 7 e 6 f 4 g 52 h 8 dtype: int32 ''' s10 = s5.loc[ 'b' : 'g' ] # print(s10) ''' b 5 c 9 d 7 e 6 f 4 g 52 dtype: int32 ''' s11 = s5.iloc[ 1 : 7 ] # print(s11) ''' b 5 c 9 d 7 e 6 f 4 g 52 dtype: int32 ''' # 4、关于NaN ''' (1)NaN是代表空值, 但不等于None。两者的数据类型不一样,None的类型为<class 'NoneType'>,而NaN的类型为<class 'float'>; (2)可以使用pd.isnull(),pd.notnull(),或自带isnull(),notnull()函数检测缺失数据 ''' # print(type(None),type(np.nan)) ''' <class 'NoneType'> <class 'float'> ''' s12 = pd.Series([ 1 , 2 , None ,np.nan],index = list ( '烽火雷电' )) # print(s12) ''' 烽 1.0 火 2.0 雷 NaN 电 NaN dtype: float64 ''' # print(pd.isnull(s12)) ''' 烽 False 火 False 雷 True 电 True dtype: bool ''' # print(pd.notnull(s12)) ''' 烽 True 火 True 雷 False 电 False dtype: bool ''' # print(s12.notnull()) ''' 烽 True 火 True 雷 False 电 False dtype: bool ''' # print(s12.isnull()) ''' 烽 False 火 False 雷 True 电 True dtype: bool ''' # 取出series中不为空的值 # print(s12[s12.notnull()]) ''' 烽 1.0 火 2.0 dtype: float64 ''' # series的name属性 ''' ''' s12.name = '风水' # print(s12) ''' 烽 1.0 火 2.0 雷 NaN 电 NaN Name: 风水, dtype: float64 ''' |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
![](http://files.jb51.net/skin/2018/images/jb51ewm.png)
微信公众号搜索 “ 脚本之家 ” ,选择关注
程序猿的那些事、送书等活动等着你
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将相关资料发送至 reterry123@163.com 进行投诉反馈,一经查实,立即处理!
最新评论