Python3.5 Pandas模块之Series用法实例分析


Posted in Python onApril 23, 2019

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

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

Python3.5 Pandas模块之Series用法实例分析

Python3.5 Pandas模块之Series用法实例分析

2、Series的创建

Python3.5 Pandas模块之Series用法实例分析

Python3.5 Pandas模块之Series用法实例分析

#!/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值的获取

Python3.5 Pandas模块之Series用法实例分析

#模块引入
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运算

Python3.5 Pandas模块之Series用法实例分析

Python3.5 Pandas模块之Series用法实例分析

#模块引入
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缺失值检验

Python3.5 Pandas模块之Series用法实例分析

Python3.5 Pandas模块之Series用法实例分析

#模块引入
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自动对齐

Python3.5 Pandas模块之Series用法实例分析

#模块引入
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属性

Python3.5 Pandas模块之Series用法实例分析

#模块引入
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程序设计有所帮助。

Python 相关文章推荐
让Python更加充分的使用Sqlite3
Dec 11 Python
python3 遍历删除特定后缀名文件的方法
Apr 23 Python
python生成lmdb格式的文件实例
Nov 08 Python
WIn10+Anaconda环境下安装PyTorch(避坑指南)
Jan 30 Python
33个Python爬虫项目实战(推荐)
Jul 08 Python
python字符串查找函数的用法详解
Jul 08 Python
Python numpy线性代数用法实例解析
Nov 15 Python
django model通过字典更新数据实例
Apr 01 Python
使用已经得到的keras模型识别自己手写的数字方式
Jun 29 Python
Python Selenium实现无可视化界面过程解析
Aug 25 Python
python 对象真假值的实例(哪些视为False)
Dec 11 Python
使用Python控制摄像头拍照并发邮件
Apr 23 #Python
详解Python静态网页爬取获取高清壁纸
Apr 23 #Python
Python matplotlib画图与中文设置操作实例分析
Apr 23 #Python
Python实现的删除重复文件或图片功能示例【去重】
Apr 23 #Python
详解Python 函数如何重载?
Apr 23 #Python
解决Django生产环境无法加载静态文件问题的解决
Apr 23 #Python
Python 存储字符串时节省空间的方法
Apr 23 #Python
You might like
制作美丽的拉花
2021/03/03 冲泡冲煮
php foreach、while性能比较
2009/10/15 PHP
PHP中根据IP地址判断城市实现城市切换或跳转代码
2012/09/04 PHP
php执行多个存储过程的方法【基于thinkPHP】
2016/11/08 PHP
PHP用mysql_insert_id()函数获得刚插入数据或当前发布文章的ID
2016/11/25 PHP
thinkphp5.1 框架导入/导出excel文件操作示例
2020/05/25 PHP
用js统计用户下载网页所需时间的脚本
2008/10/15 Javascript
JavaScript 嵌套函数指向this对象错误的解决方法
2010/03/15 Javascript
JS延迟加载加快页面打开速度示例代码
2013/12/30 Javascript
jQuery实现数秒后自动提交form的方法
2015/03/05 Javascript
JavaScript严格模式详解
2015/11/18 Javascript
基于JS实现省市联动效果代码分享
2016/06/06 Javascript
JavaScript之DOM_动力节点Java学院整理
2017/07/03 Javascript
如何以Angular的姿势打开Font-Awesome详解
2018/04/22 Javascript
vue webpack build资源相对路径的问题及解决方法
2020/06/04 Javascript
vue 监听窗口变化对页面部分元素重新渲染操作
2020/07/28 Javascript
解决pycharm双击但是无法打开的情况
2020/10/31 Javascript
vue 中的动态传参和query传参操作
2020/11/09 Javascript
WebPack工具运行原理及入门教程
2020/12/02 Javascript
教你用Type Hint提高Python程序开发效率
2016/08/08 Python
WINDOWS 同时安装 python2 python3 后 pip 错误的解决方法
2017/03/16 Python
Python实现快速计算词频功能示例
2018/06/25 Python
python3实现的zip格式压缩文件夹操作示例
2019/08/17 Python
Python MongoDB 插入数据时已存在则不执行,不存在则插入的解决方法
2019/09/24 Python
Python安装第三方库攻略(pip和Anaconda)
2020/10/15 Python
程序设计HTML5 Canvas API
2013/04/08 HTML / CSS
为智能设备设计个性化保护套网站:caseable
2017/01/05 全球购物
凯撒娱乐:Caesars Entertainment
2018/02/23 全球购物
入党申请自荐书范文
2014/02/11 职场文书
党校培训自我鉴定范文
2014/04/10 职场文书
《彩色世界》教学反思
2014/04/12 职场文书
项目经理任命书
2014/06/04 职场文书
2014年人事工作总结范文
2014/11/19 职场文书
在人间读书笔记
2015/06/30 职场文书
Python 详解通过Scrapy框架实现爬取CSDN全站热榜标题热词流程
2021/11/11 Python
一文弄懂MySQL中redo log与binlog的区别
2022/02/15 MySQL