一、创建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的更多相关文章

  1. 利用Python进行数据分析——pandas入门

    利用Python进行数据分析--pandas入门 基于NumPy建立的 from pandas importSeries,DataFrame,import pandas as pd 一.两种数据结构 ...

  2. Python 数据处理库 pandas 入门教程

    Python 数据处理库 pandas 入门教程2018/04/17 · 工具与框架 · Pandas, Python 原文出处: 强波的技术博客 pandas是一个Python语言的软件包,在我们使 ...

  3. 利用python进行数据分析之pandas入门

    转自https://zhuanlan.zhihu.com/p/26100976 目录: 5.1 pandas 的数据结构介绍5.1.1 Series5.1.2 DataFrame5.1.3索引对象5. ...

  4. 利用python进行数据分析--pandas入门2

    随书练习,第五章  pandas入门2 # coding: utf-8 # In[1]: from pandas import Series,DataFrame import pandas as pd ...

  5. 利用python进行数据分析--pandas入门1

    随书练习,第五章  pandas入门1 # coding: utf-8 # In[1]: from pandas import Series, DataFrame # In[2]: import pa ...

  6. pandas 入门(3)

    from pandas import Series, DataFrame, Index import numpy as np # 层次化索引 对数据重塑和分组操作很有用 data = Series(n ...

  7. < 利用Python进行数据分析 - 第2版 > 第五章 pandas入门 读书笔记

    <利用Python进行数据分析·第2版>第五章 pandas入门--基础对象.操作.规则 python引用.浅拷贝.深拷贝 / 视图.副本 视图=引用 副本=浅拷贝/深拷贝 浅拷贝/深拷贝 ...

  8. python数据分析之pandas库的Series应用

    一.pandas的数据结构介绍 1. Series 1.1 Series是由一种类似于一维数组的对象,它由一组数据以及一组与之相关的数据索引构成.仅由一组数据可产生最简单的Series. from p ...

  9. 二、pandas入门

    import numpy as np import pandas as pd Series: #创建Series方法1 s1=pd.Series([1,2,3,4]) s1 # 0 1 # 1 2 # ...

随机推荐

  1. 【原创】大叔经验分享(90)linux服务器iowait和负载很高

    # top top - 21:21:51 up 207 days, 1:30, 5 users, load average: 0.90, 0.79, 1.62 Tasks: 249 total, 1 ...

  2. gitea configure

    gitea configure app.ini APP_NAME = Gitea: Git with a cup of tea RUN_USER = LSGX RUN_MODE = prod [oau ...

  3. SIP:100rel 扩展

    SIP:100rel 扩展 100rel扩展即是对中间状态响应的确认(即1xx的响应码).原先在sip里,只有针对invite请求的200ok响应才会有ack,那么当中间状态响应携带重要的会话参数信息 ...

  4. 关于el-select 单选与多选切换的时候报错的解决办法

    错误: 出错原因: 估计是单选切换到多选的时候元素没有刷新的原因,猜测 解决办法: 1.在el-select上面加上一个条件判断, 条件判断中绑定一个变量值 例如 :multiple="is ...

  5. JS数组抽奖程序教学实例

    数组Javascript中非常重要的知识点,为了在课堂上提高学生兴趣,教学举例的选择就比较重要了. 为了提高学生兴趣,特设计一个可输入,可控制结束的,利用JS数组实现的抽奖教学实例.代码如下:

  6. 整理一下Promise 的用法

    Promise 的定义 Pormise是JS的异步编程的一种解决方案,在ES6将其写进了语言标准,提供了原生的Promise对象. Promise简单来理解就是一个容器,里面存放着某个未来才会结束的事 ...

  7. c#界面卡死处理方法

    方法一: 设置属性: Control.CheckForIllegalCrossThreadCalls = false; 开启一个新线程 Thread th = new Thread(() => ...

  8. Linux Backup: Hard Disk Clone with "dd"

      Most of Windows users may know "Norton Ghost". Norton Ghost is a backup software for har ...

  9. centos6/7启动故障排错

    centos6启动流程修复: 实验一:删除initramfs-2.6.32-754.el6.x86_64.img进行恢复 该文件很重要initramfs-2.6.32-754.el6.x86_64.i ...

  10. 03 WIndows编程——手绘函数调用过程

    源码 #include<Windows.h> #include<stdio.h> int MessageBoxPrint(char *szFormat, ...); int W ...