经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » Python3 » 查看文章
Python3.5 Pandas模块之Series用法实例分析
来源:jb51  时间:2019/4/24 9:45:01  对本文有异议

本文实例讲述了Python3.5 Pandas模块之Series用法。分享给大家供大家参考,具体如下:

1、Pandas模块引入与基本数据结构


2、Series的创建



  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author:ZhengzhengLiu
  4.  
  5. #模块引入
  6. import numpy as np
  7. import pandas as pd
  8. from pandas import Series,DataFrame
  9.  
  10. #1.Series通过numpy一维数组创建
  11. print("=========Series通过numpy一维数组创建==========")
  12. arr = np.array([1,2,3,4,5])
  13. s1 = pd.Series(arr)
  14. print(s1)
  15. print(s1.index)
  16. print(s1.values)
  17.  
  18. #2.Series直接通过一维数组创建
  19. print("=========Series直接通过一维数组创建==========")
  20. s2 = pd.Series([10.5,20,38,40])
  21. print(s2)
  22. #修改索引值
  23. s2.index = ['a','b','c','d']
  24. print(s2)
  25.  
  26. #Series通过一维数组创建,可以在创建的同时自定义索引值,
  27. # 也可以之后通过赋值的形式去修改
  28. print("=========Series创建的同时自定义索引值和数据类型==========")
  29. s3 = pd.Series(data=[89,78,90,87],dtype=np.float64,
  30. index=['语文','数学','英语','科学'])
  31. print(s3)
  32.  
  33. #3.Series通过字典创建,字典的键对应索引,值对应数据
  34. print("=========Series通过字典创建==========")
  35. dict = {'a':1,'b':2,"c":3,"d":4}
  36. s4 = pd.Series(dict)
  37. print(s4)
  38.  

运行结果:

=========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. import numpy as np
  3. import pandas as pd
  4. from pandas import Series,DataFrame
  5.  
  6. #4.Series值的获取
  7. print("=========Series值的获取==========")
  8. s2 = pd.Series([10.5,20,38,40])
  9. #修改索引值
  10. s2.index = ['a','b','c','d']
  11. print(s2)
  12. print(s2[0]) #方括号+下标值的形式获取Series值
  13. print(s2["a"]) #方括号+索引的形式获取Series值
  14.  

运行结果:

=========Series值的获取==========
a    10.5
b    20.0
c    38.0
d    40.0
dtype: float64
10.5
10.5

4、Series运算



  1. #模块引入
  2. import numpy as np
  3. import pandas as pd
  4. from pandas import Series,DataFrame
  5.  
  6. #5.Series值的运算
  7. #Series中元素级别的运算结果,包含索引值并且键值关系保持不变
  8. print("=========Series值的运算==========")
  9. s6 = pd.Series({'a':1,'b':2,"c":3,"d":4})
  10. print(s6)
  11. print("=========打印Series大于2的值==========")
  12. print(s6[s6>2])
  13. print("=========打印Series的值除以2==========")
  14. print(s6/2)
  15.  
  16. #numpy中的通用函数在Series中也支持
  17. s7= pd.Series([1,2,-3,-4])
  18. 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. import numpy as np
  3. import pandas as pd
  4. from pandas import Series,DataFrame
  5.  
  6. #6.Series缺失值检验
  7. scores = Series({"a":88,"b":79,"c":98,"d":100})
  8. print(scores)
  9.  
  10. new = ["a","b","e","c","d"]
  11. scores = Series(scores,index=new)
  12. print(scores)
  13.  
  14. print("======过滤出为缺失值的项=======")
  15. print(scores.isnull()) #NAN值返回True
  16. #print(pd.isnull(scores)) #与上面一句等价
  17.  
  18. print("======过滤出为非缺失值的项=======")
  19. 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. import numpy as np
  3. import pandas as pd
  4. from pandas import Series,DataFrame
  5.  
  6. #7.Series自动对齐
  7.  
  8. s8 = Series([12,28,46],index=["p1","p2","p3"])
  9. s9 = Series([2,4,6,8],index=["p2","p3","p4","p5"])
  10. print("=======s8=======")
  11. print(s8)
  12. print("=======s9=======")
  13. print(s9)
  14. print("=======s8+s9=======")
  15. print(s8+s9)
  16.  

运行结果:

=======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. import numpy as np
  3. import pandas as pd
  4. from pandas import Series,DataFrame
  5.  
  6. #8.Series及其name属性
  7. s10 = Series({"jack":18,"amy":20,"lili":23,"susan":15})
  8. print(s10)
  9.  
  10. print("=======设置name属性后=======")
  11. s10.name = "年龄" #数据名称标签
  12. s10.index.name = "姓名" #索引名称标签
  13.  
  14. print(s10)
  15.  

运行结果:

amy      20
jack     18
lili     23
susan    15
dtype: int64
=======设置name属性后=======
姓名
amy      20
jack     18
lili     23
susan    15
Name: 年龄, dtype: int64

更多关于Python相关内容感兴趣的读者可查看jb51专题:《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总

希望本文所述对大家Python程序设计有所帮助。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号