本文实例讲述了python3.5 pandas模块之series用法。分享给大家供大家参考,具体如下:
1、pandas模块引入与基本数据结构
2、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
|
#!/usr/bin/env python # -*- coding:utf-8 -*- # author:zhengzhengliu #模块引入 import numpy as np import pandas as pd from pandas import series,dataframe #1.series通过numpy一维数组创建 print ( "=========series通过numpy一维数组创建==========" ) arr = np.array([ 1 , 2 , 3 , 4 , 5 ]) s1 = pd.series(arr) print (s1) print (s1.index) print (s1.values) #2.series直接通过一维数组创建 print ( "=========series直接通过一维数组创建==========" ) s2 = pd.series([ 10.5 , 20 , 38 , 40 ]) print (s2) #修改索引值 s2.index = [ 'a' , 'b' , 'c' , 'd' ] print (s2) #series通过一维数组创建,可以在创建的同时自定义索引值, # 也可以之后通过赋值的形式去修改 print ( "=========series创建的同时自定义索引值和数据类型==========" ) s3 = pd.series(data = [ 89 , 78 , 90 , 87 ],dtype = np.float64, index = [ '语文' , '数学' , '英语' , '科学' ]) print (s3) #3.series通过字典创建,字典的键对应索引,值对应数据 print ( "=========series通过字典创建==========" ) dict = { 'a' : 1 , 'b' : 2 , "c" : 3 , "d" : 4 } s4 = pd.series( dict ) print (s4) |
运行结果:
=========series通过numpy一维数组创建==========
0 1
1 2
2 3
3 4
4 5
dtype: int32
rangeindex(start=0, stop=5, step=1)
[1 2 3 4 5]
=========series直接通过一维数组创建==========
0 10.5
1 20.0
2 38.0
3 40.0
dtype: float64
a 10.5
b 20.0
c 38.0
d 40.0
dtype: float64
=========series创建的同时自定义索引值和数据类型==========
语文 89.0
数学 78.0
英语 90.0
科学 87.0
dtype: float64
=========series通过字典创建==========
a 1
b 2
c 3
d 4
dtype: int64
3、series值的获取
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#模块引入 import numpy as np import pandas as pd from pandas import series,dataframe #4.series值的获取 print ( "=========series值的获取==========" ) s2 = pd.series([ 10.5 , 20 , 38 , 40 ]) #修改索引值 s2.index = [ 'a' , 'b' , 'c' , 'd' ] print (s2) print (s2[ 0 ]) #方括号+下标值的形式获取series值 print (s2[ "a" ]) #方括号+索引的形式获取series值 |
运行结果:
=========series值的获取==========
a 10.5
b 20.0
c 38.0
d 40.0
dtype: float64
10.5
10.5
4、series运算
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#模块引入 import numpy as np import pandas as pd from pandas import series,dataframe #5.series值的运算 #series中元素级别的运算结果,包含索引值并且键值关系保持不变 print ( "=========series值的运算==========" ) s6 = pd.series({ 'a' : 1 , 'b' : 2 , "c" : 3 , "d" : 4 }) print (s6) print ( "=========打印series大于2的值==========" ) print (s6[s6> 2 ]) print ( "=========打印series的值除以2==========" ) print (s6 / 2 ) #numpy中的通用函数在series中也支持 s7 = pd.series([ 1 , 2 , - 3 , - 4 ]) print (np.exp(s7)) |
运行结果:
=========series值的运算==========
a 1
b 2
c 3
d 4
dtype: int64
=========打印series大于2的值==========
c 3
d 4
dtype: int64
=========打印series的值除以2==========
a 0.5
b 1.0
c 1.5
d 2.0
dtype: float64
0 2.718282
1 7.389056
2 0.049787
3 0.018316
dtype: float64
5、series缺失值检验
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
#模块引入 import numpy as np import pandas as pd from pandas import series,dataframe #6.series缺失值检验 scores = series({ "a" : 88 , "b" : 79 , "c" : 98 , "d" : 100 }) print (scores) new = [ "a" , "b" , "e" , "c" , "d" ] scores = series(scores,index = new) print (scores) print ( "======过滤出为缺失值的项=======" ) print (scores.isnull()) #nan值返回true #print(pd.isnull(scores)) #与上面一句等价 print ( "======过滤出为非缺失值的项=======" ) print (pd.notnull(scores)) #非nan值返回true |
运行结果:
a 88
b 79
c 98
d 100
dtype: int64
a 88.0
b 79.0
e nan
c 98.0
d 100.0
dtype: float64
======过滤出为缺失值的项=======
a false
b false
e true
c false
d false
dtype: bool
======过滤出为非缺失值的项=======
a true
b true
e false
c true
d true
dtype: bool
6、series自动对齐
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#模块引入 import numpy as np import pandas as pd from pandas import series,dataframe #7.series自动对齐 s8 = series([ 12 , 28 , 46 ],index = [ "p1" , "p2" , "p3" ]) s9 = series([ 2 , 4 , 6 , 8 ],index = [ "p2" , "p3" , "p4" , "p5" ]) print ( "=======s8=======" ) print (s8) print ( "=======s9=======" ) print (s9) print ( "=======s8+s9=======" ) print (s8 + s9) |
运行结果:
=======s8=======
p1 12
p2 28
p3 46
dtype: int64
=======s9=======
p2 2
p3 4
p4 6
p5 8
dtype: int64
=======s8+s9=======
p1 nan
p2 30.0
p3 50.0
p4 nan
p5 nan
dtype: float64
7、series及其索引的name属性
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#模块引入 import numpy as np import pandas as pd from pandas import series,dataframe #8.series及其name属性 s10 = series({ "jack" : 18 , "amy" : 20 , "lili" : 23 , "susan" : 15 }) print (s10) print ( "=======设置name属性后=======" ) s10.name = "年龄" #数据名称标签 s10.index.name = "姓名" #索引名称标签 print (s10) |
运行结果:
amy 20
jack 18
lili 23
susan 15
dtype: int64
=======设置name属性后=======
姓名
amy 20
jack 18
lili 23
susan 15
name: 年龄, dtype: int64
希望本文所述对大家python程序设计有所帮助。
原文链接:https://blog.csdn.net/loveliuzz/article/details/78498077