目录
#index修改为非数字类型的索引:①按照修改后的index索引,②按照默认的index索引
#index修改为其他型数据的索引:按照修改后的index索引
4.1、修改Series的values类型:Seriesname.astype(dtype)
Series类型类似于Numpy的一维数组对象,可以将该类型看做是一组数据与数据相关的标签(索引)联合而构成(带有标签的一维数组对象)。
Series数组对象由两部分构成:
值(value):一维数组的各元素值,是一个ndarray类型数据。
索引(index):与一维数组值一一对应的标签。利用索引,我们可非常方便得在Series数组中进行取值。
2、Series的创建方式
Series常用的创建(初始化)方式:
- 列表等可迭代对象
- ndarray数组对象
- 字典对象
- 标量
如果创建Series不指定index,则生成默认的index(0 ~ n-1)
# 使用列表,创建Series
In [1]: import numpy as np
In [2]: import pandas as pd
In [3]: s = pd.Series([1,2,3,4])
In [4]: print(s)
0 1
1 2
2 3
3 4
dtype: int64
In [5]: print(type(s))
<class 'pandas.core.series.Series'>
虽然我们在生成的时候没有设置index值,但Series还是会自动帮我们生成index,这种方式生成的Series结构跟list列表差不多,可以把这种形式的Series理解为竖起来的list列表,左边是index,右边是元素。
# 自己指定index,和数据类型,创建Series
In [1]: import pandas as pd
In [2]: import numpy as np
In [3]: s = pd.Series([1,2,3,4],index = ['A','B','C','D'],dtype=np.int32)
In [4]: print(s,type(s))
A 1
B 2
C 3
D 4
dtype: int32 <class 'pandas.core.series.Series'>
In [5]: s1 = pd.Series(range(1,10,2),index =list("abcde"),dtype=np.int32)
In [6]: print(type(s1),'\n',s)
<class 'pandas.core.series.Series'>
A 1
B 2
C 3
D 4
dtype: int32
In [7]: import string
In [8]: t = pd.Series(data=np.arange(10),index=list(string.ascii_uppercase[:10]))
In [9]: print(t)
A 0
B 1
C 2
D 3
E 4
F 5
G 6
H 7
I 8
J 9
dtype: int32
#通过字典创建Series(注意:字典中的键就是索引)
In [3]: d = {"name":"jalen","sex":"men","age":27}
In [4]: s = pd.Series(d)
In [5]: print(type(s),s)
<class 'pandas.core.series.Series'>
name jalen
sex men
age 27
dtype: object
In [12]: import string
In [13]: s = pd.Series({string.ascii_uppercase[i]:i for i in range(5)})
In [14]: print(s)
A 0
B 1
C 2
D 3
E 4
dtype: int64
3、Series索引和值
In [1]: import numpy as np
In [2]: import pandas as pd
In [3]: s = pd.Series(np.random.random((10)),index=list('abcdefghij'),dtype=np.float32)
In [4]: print(s.index)
Index(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'], dtype='object')
In [5]: li= []
In [6]: for i in s.index:
...: li.append(i)
...:
In [7]: print(li)
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
In [8]: print(list(s.index))
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
In [9]: print(len(s.index))
10
In [10]: print(list(s.index)[:2])
['a', 'b']
In [11]: print(s.values)
[0.5047071 0.6553416 0.07418957 0.5746948 0.27903566 0.3718615
0.66567683 0.31206223 0.76903474 0.8105177 ]
In [12]: print(type(s.values))
<class 'numpy.ndarray'>
In [13]: print(s)
a 0.504707
b 0.655342
c 0.074190
d 0.574695
e 0.279036
f 0.371861
g 0.665677
h 0.312062
i 0.769035
j 0.810518
dtype: float32
3.1、通过索引获取单个元素的值
#默认index的索引
In [49]: s = pd.Series(range(5,10))
In [50]: print(s)
0 5
1 6
2 7
3 8
4 9
dtype: int64
In [51]: print(s[0],s[1],s[2],s[3],s[4])
5 6 7 8 9
#index修改为非数字类型的索引:①按照修改后的index索引,②按照默认的index索引
In [13]: s = pd.Series(list(np.random.rand(5)),index=list("abcde"),dtype=np.float64)
In [14]: print(s)
a 0.462889
b 0.174131
c 0.663830
d 0.894193
e 0.000991
dtype: float64
In [15]: print(s[3],s['d'])
0.8941932513259072 0.8941932513259072
#index修改为其他型数据的索引:按照修改后的index索引
In [20]: s = pd.Series(np.random.random((10)),index=range(20,30),dtype=np.float64)
In [21]: print(s[20],s[21],'...',s[29])
0.41507467921400365 0.2974002495671728 ... 0.17945114450242472
3.2、通过切片或索引获取多个元素的值
In [20]: s = pd.Series(np.random.random((10)),index=range(20,30),dtype=np.float64)
#切片
#语法:s[起始:终止:步长] <步长=2,表示相隔1个元素后取值,步长=n,表示相隔n-1个元素后取值>
In [22]: print(s[0:3]) #取index=[0,3)的元素
20 0.415075
21 0.297400
22 0.708073
dtype: float64
In [23]: print(s[:3]) #取index=[起始,3)的元素
20 0.415075
21 0.297400
22 0.708073
dtype: float64
In [25]: print(s[7:]) #取index=[7,最后]的元素
27 0.825456
28 0.507532
29 0.179451
dtype: float64
In [26]: print(s[0:10:3]) #取index=[0,3,6,9]的元素
20 0.415075
23 0.968407
26 0.157795
29 0.179451
dtype: float64
In [27]: print(s[:9:3]) #取index=[0,3,6]的元素
20 0.415075
23 0.968407
26 0.157795
dtype: float64
In [28]: print(s[::3]) #取index=[0,3,6,9]的元素
20 0.415075
23 0.968407
26 0.157795
29 0.179451
dtype: float64
In [37]: s = pd.Series(np.random.random((10)),index=list('abcdefghij'),dtype=np.float64)
#切片
#语法:s[起始:终止:步长] <步长=2,表示相隔1个元素后取值,步长=n,表示相隔n-1个元素后取值>
In [38]: print(s[::])
a 0.964390
b 0.871407
c 0.764161
d 0.970029
e 0.457365
f 0.121668
g 0.581348
h 0.627128
i 0.577417
j 0.137327
dtype: float64
In [39]: print(s[:'c'])
a 0.964390
b 0.871407
c 0.764161
dtype: float64
In [40]: print(s[:3])
a 0.964390
b 0.871407
c 0.764161
dtype: float64
In [42]: print(s['h':])
h 0.627128
i 0.577417
j 0.137327
dtype: float64
In [43]: print(s['a':'h':3])
a 0.964390
d 0.970029
g 0.581348
dtype: float64
In [56]: s = pd.Series(np.random.random((10)),index=list('abcdefghij'),dtype=np.float64)
#索引
#语法:s[[inx1,inx2,inx4,...]]
In [57]: print(s[[1,5,6,7]])
b 0.354418
f 0.266886
g 0.393644
h 0.737391
dtype: float64
In [58]: print(s[['a','b','f','h']])
a 0.153360
b 0.354418
f 0.266886
h 0.737391
dtype: float64
In [67]: s = pd.Series(np.random.random((10)),index=list('abcdefghij'),dtype=np.float64)
#布尔索引
In [68]: print(s[(s>0.3) & (s<0.5)])
a 0.470118
c 0.473853
h 0.400064
dtype: float64
In [69]: print(s[~(s>0.3)])
d 0.104811
e 0.112435
g 0.170451
dtype: float64
In [70]: print(s[(s<0.3) | (s>0.7)])
b 0.728800
d 0.104811
e 0.112435
g 0.170451
i 0.712267
dtype: float64
4、修改Series
4.1、修改Series的values类型:Seriesname.astype(dtype)
In [36]: s = pd.Series(range(5))
In [37]: print(s)
0 0
1 1
2 2
3 3
4 4
dtype: int64 #dtype: int64
In [38]: s.astype(np.float32)
Out[38]:
0 0.0
1 1.0
2 2.0
3 3.0
4 4.0
dtype: float32 #dtype: float32
In [39]: print(s)
0 0
1 1
2 2
3 3
4 4
dtype: int64 #dtype: int64
In [40]: s = s.astype(np.float32)
In [41]: print(s)
0 0.0
1 1.0
2 2.0
3 3.0
4 4.0
dtype: float32 #dtype: float32
注意:修改了type后需要赋值给原Series才生效。
4.2、修改Series的元素的值
注意:修改时数据的type保持一致,通过索引、切片等方法获得元素的值,将新的值赋值给对应元素即可。
In [17]: s = pd.Series(np.random.random((10)),index=list('abcdefghij'),dtype=np.float32)
In [18]: print(s)
a 0.311215
b 0.648675
c 0.432390
d 0.293076
e 0.387463
f 0.102538
g 0.467891
h 0.230558
i 0.589298
j 0.069600
dtype: float32
In [19]: s[0]=0
In [20]: print(s)
a 0.000000
b 0.648675
c 0.432390
d 0.293076
e 0.387463
f 0.102538
g 0.467891
h 0.230558
i 0.589298
j 0.069600
dtype: float32
In [21]: s['j']=1.0
In [22]: print(s)
a 0.000000
b 0.648675
c 0.432390
d 0.293076
e 0.387463
f 0.102538
g 0.467891
h 0.230558
i 0.589298
j 1.000000
dtype: float32
In [23]: s[(s<0.2)] = 0
In [24]: print(s)
a 0.000000
b 0.648675
c 0.432390
d 0.293076
e 0.387463
f 0.000000
g 0.467891
h 0.230558
i 0.589298
j 1.000000
dtype: float32