pandas入门之Series
一、创建Series
参数
- Series (Series)是能够保存任何类型的数据(整数,字符串,浮点数,Python对象等)的一维标记数组。轴标签统称为索引。
- data 参数
- index 索引 索引值必须是唯一的和散列的,与数据的长度相同。 默认np.arange(n)如果没有索引被传递。
- dtype 输出的数据类型 如果没有,将推断数据类型
- copy 复制数据 默认为false
数组创建
data = ['a','b','c','d','e']
res= pd.Series(data,index=[i for i in range(1,6)],dtype=str)
print(res) 1 a
2 b
3 c
4 d
5 e
dtype: object
字典创建
data = {"a":1.,"b":2,"c":3,"d":4}
res = pd.Series(data,index=["d","c","b","a"])
print(res) # 字典的键用于构建索引
d 4.0
c 3.0
b 2.0
a 1.0
dtype: float64
常量创建
# 如果数据是常量值,则必须提供索引。将重复该值以匹配索引的长度。
res = pd.Series(5,index=[1,2,3,4,5])
print(res) 1 5
2 5
3 5
4 5
5 5
dtype: int64
二、数据查询
切片
data = [1,2,3,4,5]
res = pd.Series(data,index=["a","b","c","d","e"])
print(res[0:3],"---") # 这里跟python的切片一样
print(res[3],"---")
print(res[-3:],"---") a 1
b 2
c 3
dtype: int64 --- 4 --- c 3
d 4
e 5
dtype: int64 ---
使用索引检索数据
data = [1,2,3,4,5]
res = pd.Series(data,index=["a","b","c","d","e"])
print(res["a"])
# 检索多个值 标签用中括号包裹
print(res[["a","b"]]) # 如果用没有的标签检索则会抛出异常KeyError: 'f' 1 a 1
b 2
dtype: int64
data = [1,2,3,4,5]
res = pd.Series(data)
res[[2,4]] 2 3
4 5
dtype: int64
使用head()/tail()查看前几个或后几个
data = [1,2,3,4,5]
res = pd.Series(data,index=["a","b","c","d","e"])
res.head(3) # 查看前三个
res.tail(2) # 查看后两个
三、其他操作
series元素进行去重
unique() 对series元素进行去重
s = pd.Series(data=[1,1,2,2,3,4,5,6,6,6,7,6,6,7,8])
s.unique() array([1, 2, 3, 4, 5, 6, 7, 8], dtype=int64)
两个series元素相加
Series之间的运算
- 在运算中自动对齐不同索引的数据
- 如果索引不对应,则补NaN
# 当索引没有对应的值时,可能出现缺失数据显示NaN(not a number)的情况
s1 = pd.Series(data=[1,2,3,4,5],index=["a","b","c","d","e"])
s2 = pd.Series(data=[1,2,3,4,5],index=["a","b","c","d","f"])
s = s1 + s2
s a 2.0
b 4.0
c 6.0
d 8.0
e NaN
f NaN
dtype: float64
监测缺失的数据
isnull() # 缺失的数据返回的布尔值为True
notnull() # 缺失的数据返回的布尔值为False
isnull
s1 = pd.Series(data=[1,2,3,4,5],index=["a","b","c","d","e"])
s2 = pd.Series(data=[1,2,3,4,5],index=["a","b","c","d","f"])
s = s1 + s2
s.isnull() # 缺失的数据返回的布尔值为True a False
b False
c False
d False
e True
f True
dtype: bool
notnull
s1 = pd.Series(data=[1,2,3,4,5],index=["a","b","c","d","e"])
s2 = pd.Series(data=[1,2,3,4,5],index=["a","b","c","d","f"])
s = s1 + s2
s.notnull() # 缺失的数据返回的布尔值为False a True
b True
c True
d True
e False
f False
dtype: bool
如果将布尔值作为Serrise的索引,则只保留True对应的元素值
s[[True,True,False,False,True,True]] a 2.0
b 4.0
e NaN
f NaN
dtype: float64
根据上面的特性,可以取出所有空的数据和所有不为空的数据
s[s.isnull()] # 取所有空值 e NaN
f NaN
dtype: float64 s[s.notnull()] # 取出不为空的数据 a 2.0
b 4.0
c 6.0
d 8.0
dtype: float64 s.index # 取出索引 Index(['a', 'b', 'c', 'd', 'e', 'f'], dtype='object')
pandas入门之Series的更多相关文章
- 利用Python进行数据分析——pandas入门
利用Python进行数据分析--pandas入门 基于NumPy建立的 from pandas importSeries,DataFrame,import pandas as pd 一.两种数据结构 ...
- Python 数据处理库 pandas 入门教程
Python 数据处理库 pandas 入门教程2018/04/17 · 工具与框架 · Pandas, Python 原文出处: 强波的技术博客 pandas是一个Python语言的软件包,在我们使 ...
- 利用python进行数据分析之pandas入门
转自https://zhuanlan.zhihu.com/p/26100976 目录: 5.1 pandas 的数据结构介绍5.1.1 Series5.1.2 DataFrame5.1.3索引对象5. ...
- 利用python进行数据分析--pandas入门2
随书练习,第五章 pandas入门2 # coding: utf-8 # In[1]: from pandas import Series,DataFrame import pandas as pd ...
- 利用python进行数据分析--pandas入门1
随书练习,第五章 pandas入门1 # coding: utf-8 # In[1]: from pandas import Series, DataFrame # In[2]: import pa ...
- pandas 入门(3)
from pandas import Series, DataFrame, Index import numpy as np # 层次化索引 对数据重塑和分组操作很有用 data = Series(n ...
- < 利用Python进行数据分析 - 第2版 > 第五章 pandas入门 读书笔记
<利用Python进行数据分析·第2版>第五章 pandas入门--基础对象.操作.规则 python引用.浅拷贝.深拷贝 / 视图.副本 视图=引用 副本=浅拷贝/深拷贝 浅拷贝/深拷贝 ...
- python数据分析之pandas库的Series应用
一.pandas的数据结构介绍 1. Series 1.1 Series是由一种类似于一维数组的对象,它由一组数据以及一组与之相关的数据索引构成.仅由一组数据可产生最简单的Series. from p ...
- 二、pandas入门
import numpy as np import pandas as pd Series: #创建Series方法1 s1=pd.Series([1,2,3,4]) s1 # 0 1 # 1 2 # ...
随机推荐
- (三)自定义Realm
一.Realm概念 Realm:域,Shiro从从Realm获取安全数据(如用户.角色.权限),就是说SecurityManager要验证用户身份,那么它需要从Realm获取相应的用户进行比较以确定用 ...
- SqlServer 多服务器管理配置报错:Ensure the agent startup account for 'x.x.x.x' has rights to login as target server, Access is denied.
SQL Server 2012配置多服务器管理时,SSMS设置一直报错,配置失败: 解决方法: 1. 为SQL Server Agent单独创建一个账号,主服务器和目标服务器都创建一样的账号 2. 把 ...
- 八、wepy代码规范
变量与方法尽量使用驼峰式命名,并且注意避免使用$开头. 以$开头的标识符为WePY框架的内建属性和方法,可在JavaScript脚本中以this.的方式直接使用,具体请参考API文档. 小程序入口.页 ...
- springboot启动流程(二)SpringApplication run方法核心逻辑
所有文章 https://www.cnblogs.com/lay2017/p/11478237.html run方法逻辑 在上一篇文章中,我们看到SpringApplication的静态方法最终是去构 ...
- cygwin_exception::open_stackdumpfile: Dumping stack trace to HttpServer.exe.stackdump错误
本来,我在Windows下使用Cygwin编译运行c程序,在执行*.exe时报出如题错误,我在Linux环境下使用gcc编译运行,则正常. 所以,当你无法解决上述问题时,换系统吧!
- centos安装netcat工具及测试
netcat是网络工具中的瑞士军刀,它能通过TCP和UDP在网络中读写数据.通过与其他工具结合和重定向,你可以在脚本中以多种方式使用它.使用netcat命令所能完成的事情令人惊讶. netcat所做的 ...
- selenium重定向新窗口
1.跳转新窗口 # 浏览器跳转新窗口后,selenium绑定新窗口 print('页面跳转后重新绑定selenium.') time.sleep(3) search_window = driver.c ...
- linux使用glibc版本安装mysql8.0.12
1.前言 使用yum安装虽然很方便,但是如果要是在没有公网的环境下,是没有办法使用yum源的.所以我们可以使用mysql提供的glibc版本的安装包,进行安装. 但是在安装之前,一定要将以前的版本删除 ...
- linux——运维基础,与常用命令
1 运维概述 1 什么是运维 服务器的运行维护 2 名词 IDC(互联网数据中心) 3 监控软件 zabbix(用的最多), nagios, cactti 4 常用的linux操作系统 1 CentO ...
- Spark(二)算子详解
目录 Spark(二)算子讲解 一.wordcountcount 二.编程模型 三.RDD数据集和算子的使用 Spark(二)算子讲解 @ 一.wordcountcount 基于上次的wordcoun ...