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 # ...
随机推荐
- my SO 链接opencv静态库一些FUCKing的笔记 opencv410 有毒
1. 2. CMake "/work/lib/opencv/ubuntu14/4.1.0" make[2]: *** No rule to make target `/usr/lo ...
- TreeMap核心源码实现解析
TreeMap实现了SotredMap接口,它是有序的集合.而且是一个红黑树结构,每个key-value都作为一个红黑树的节点.如果在调用TreeMap的构造函数时没有指定比较器,则根据key执行自然 ...
- [LeetCode] 46. Int数组全排列 ☆☆☆(回溯)
描述 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2, ...
- 【Linux】Linux基本命令
一.Linux关机 shutdown -h 10 10min后关机 shutdown -h 10:00 10:00关机 shutdown -h now 或 halt 或 poweroff 立即关机 ...
- Linux shell循环遍历
有时候需要紧急处理一些Excel列表中的数据,如提供一堆id列表,需要删除对应的表,一开始的办法是通过python pandas读取excel,然后拼接id元祖执行sql命令: 运维的同事说不用这么麻 ...
- Link monitoring
参考:https://www.ibm.com/support/knowledgecenter/en/linuxonibm/com.ibm.linux.z.l0wlcb00/l0wlcb00_miimo ...
- 内核对象&句柄
目录 1 内核对象的概念 2 内核对象的使用计数 3 句柄 4 句柄表 项目工程代码中设计句柄的使用,一时不知句柄是何物,通过查阅自学之后,对句柄及其使用有一个初步的了解.分享出来,算是抛砖引玉吧 ...
- PAT Basic 1078 字符串压缩与解压 (20 分)
文本压缩有很多种方法,这里我们只考虑最简单的一种:把由相同字符组成的一个连续的片段用这个字符和片段中含有这个字符的个数来表示.例如 ccccc 就用 5c 来表示.如果字符没有重复,就原样输出.例如 ...
- Ubuntu系统---C++之Eclipse IDE 编译器安装
Ubuntu系统---C++之Eclipse IDE 编译器安装 Eclipse是一个基于Java的.开放源码的.可扩展的应用开发平台,它为编程人员提供了一流的Java集成开发环境(Integrate ...
- python 学习笔记_3 输入字母,打印出要输入的星期几; 首字母无效,则继续输入,最多2次即可判断结果;否则退出。
#coding=gbk ''' 输入字母,打印出要输入的星期几: 首字母无效,则继续输入,最多2次即可判断结果:否则退出. ''' week_list=['monday','tuesday','wed ...