Pandas用法

import pandas as pd
import numpy as np

1.创建Series

1)创建一个空Series

s = pd.Series()
s
<ipython-input-3-85850638a114>:1: DeprecationWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.s = pd.Series()Series([], dtype: float64)

2)从ndarray创建一个Series,并规定索引为[100,101,102,103]

arr = np.array(['a','b','c','d'])
ser1 = pd.Series(arr,index=[100,101,102,103])
ser1
100    a
101    b
102    c
103    d
dtype: object

3)从字典创建一个Series,字典键用于构建索引

dic = {'a':0,'b':1,'c':2,'d':3}
dic
{'a': 0, 'b': 1, 'c': 2, 'd': 3}
ser2 = pd.Series(dic)
ser2
a    0
b    1
c    2
d    3
dtype: int64

4) 从标量创建一个Series,此时,必须提供索引,重复值以匹配索引的长度

s = pd.Series(5,index = [0,1,2,3])
s
0    5
1    5
2    5
3    5
dtype: int64

2.从具体位置的Series中访问数据(pandas切片)

1) 检索Series中的第一个元素

s=pd.Series([1,2,3,4,5],index=['a','b','c','d','e'])
s
a    1
b    2
c    3
d    4
e    5
dtype: int64
s[0]
1

2) 检索Series中的前三个元素

s[:3]
a    1
b    2
c    3
dtype: int64

3) 检索Series中最后三个元素

s[2:]
c    3
d    4
e    5
dtype: int64
s[-3:]
c    3
d    4
e    5
dtype: int64

3.使用标签检索数据(索引):一个Series就像一个固定大小的字典,可以通过索引标签获取和设置值

1) 使用索引标签检索单个元素

s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
s
a    1
b    2
c    3
d    4
e    5
dtype: int64
s['b']
2

2) 使用索引标签列表检索多个元素

s1 = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
s1
a    1
b    2
c    3
d    4
e    5
dtype: int64
print(['a','b','c','d'])
['a', 'b', 'c', 'd']

3) 如果不包含标签,检索会出现异常

s['f']
---------------------------------------------------------------------------KeyError                                  Traceback (most recent call last)C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)2894             try:
-> 2895                 return self._engine.get_loc(casted_key)2896             except KeyError as err:pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()KeyError: 'f'The above exception was the direct cause of the following exception:KeyError                                  Traceback (most recent call last)<ipython-input-24-c23937ec966b> in <module>
----> 1 s['f']C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py in __getitem__(self, key)880 881         elif key_is_scalar:
--> 882             return self._get_value(key)883 884         if is_hashable(key):C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\series.py in _get_value(self, label, takeable)987 988         # Similar to Index.get_value, but we do not fall back to positional
--> 989         loc = self.index.get_loc(label)990         return self.index._get_values_for_loc(self, loc, label)991 C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)2895                 return self._engine.get_loc(casted_key)2896             except KeyError as err:
-> 2897                 raise KeyError(key) from err2898 2899         if tolerance is not None:KeyError: 'f'

4.简单运算

在pandas的Series中,会保留NumPy的数组操作(用布尔数组过滤数据,标量乘法,以及使用数学函数),并同时保持引用的使用

1) 例

ser2 = pd.Series(range(4),index = ["a","b","c","d"])
ser2
a    0
b    1
c    2
d    3
dtype: int64
ser2[ser2 > 2]
d    3
dtype: int64
ser2 * 2
a    0
b    2
c    4
d    6
dtype: int64
np.exp(ser2)
a     1.000000
b     2.718282
c     7.389056
d    20.085537
dtype: float64

Series的自动对齐Series的一个重要功能就是自动对齐(不明觉厉),看看例子就明白了。 差不多就是不同Series对象运算的时候根据其索引进行匹配计算

1)创建两个Series名为ser3与ser4.

sdata = {'Ohio': 35000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000}
sdata
{'Ohio': 35000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000}
ser3 = pd.Series(sdata)
ser3
Ohio      35000
Texas     71000
Oregon    16000
Utah       5000
dtype: int64
states = ['California', 'Ohio', 'Oregon', 'Texas']
states
['California', 'Ohio', 'Oregon', 'Texas']
ser4 = pd.Series(sdata,index = states)
ser4
California        NaN
Ohio          35000.0
Oregon        16000.0
Texas         71000.0
dtype: float64
 ser3+ser4
California         NaN
Ohio           70000.0
Oregon         32000.0
Texas         142000.0
Utah               NaN
dtype: float64

5.Series增删改

1) 增:Series的add()方法是加法计算不是增加Series元素用的,使用append连接其他Series。

sdata = {'Ohio': 35000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000}
sdata
{'Ohio': 35000, 'Texas': 71000, 'Oregon': 16000, 'Utah': 5000}
ser3 = pd.Series(sdata)
ser3
Ohio      35000
Texas     71000
Oregon    16000
Utah       5000
dtype: int64
states = ['California', 'Ohio', 'Oregon', 'Texas']
states
['California', 'Ohio', 'Oregon', 'Texas']
ser4 = pd.Series(sdata,index = states)
ser4
California        NaN
Ohio          35000.0
Oregon        16000.0
Texas         71000.0
dtype: float64
ser3.append(ser4)
Ohio          35000.0
Texas         71000.0
Oregon        16000.0
Utah           5000.0
California        NaN
Ohio          35000.0
Oregon        16000.0
Texas         71000.0
dtype: float64

2)删:Series的drop()方法可以对Series进行删除操作,返回一个被删除后的Series,原来的Series不改变

s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
s
a    1
b    2
c    3
d    4
e    5
dtype: int64
a1 = s.drop('a')
s
a    1
b    2
c    3
d    4
e    5
dtype: int64
a1
b    2
c    3
d    4
e    5
dtype: int64

3) 改:通过索引的方式查找到某个元素,然后通过“=”赋予新的值

s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
s
a    1
b    2
c    3
d    4
e    5
dtype: int64
s['a']=5
s
a    5
b    2
c    3
d    4
e    5
dtype: int64

pandasSeries模块相关推荐

  1. etcd 笔记(05)— etcd 代码结构、各模块功能、整体架构、各模块之间的交互、请求和应答流程

    1. etcd 项目结构和功能 etcd 项目代码的目录结构如下: $ tree ├── auth ├── build ├── client ├── clientv3 ├── contrib ├── ...

  2. OpenCV 笔记(01)— OpenCV 概念、整体架构、各模块主要功能

    1. OpenCV 概念 图像处理( Image Processing )是用计算机对图像进行分析, 以达到所需结果的技术, 又称影像处理. 图像处理技术一般包括图像压缩, 增强和复原, 匹配.描述和 ...

  3. Python 多线程总结(1)- thread 模块

    thread 模块 1. 单线程 首先看下单线程程序运行的例子,如下所示, import timedef loop0():print 'start loop0 begin', time.ctime() ...

  4. 关于python导入模块和package的一些深度思考

    背景 在python中有导入模块和导入package一说,这篇文章主要介绍导入模块和package的一些思考. 首先什么是模块?什么是package? 模块:用来从逻辑上组织python代码(变量,函 ...

  5. Python Re 模块超全解读!详细

    内行必看!Python Re 模块超全解读! 2019.08.08 18:59:45字数 953阅读 121 re模块下的函数 compile(pattern):创建模式对象 > import ...

  6. python性能分析之line_profiler模块-耗时,效率 时间

    20210203 直接用pycharm 自带的 20201215 直接装不上的情况下 先下载安装文件 再安装 line_profiler使用装饰器(@profile)标记需要调试的函数.用kernpr ...

  7. python:Json模块dumps、loads、dump、load介绍

    20210831 https://www.cnblogs.com/bigtreei/p/10466518.html json dump dumps 区别 python:Json模块dumps.load ...

  8. 关于python 中的__future__模块

    Python的每个新版本都会增加一些新的功能,或者对原来的功能作一些改动.有些改动是不兼容旧版本的,也就是在当前版本运行正常的代码,到下一个版本运行就可能不正常了. 具体说来就是,某个版本中出现了某个 ...

  9. GPUtil是一个Python模块,使用nvidia-smi从NVIDA GPU获取GPU状态

    GPUtil是一个Python模块,使用nvidia-smi从NVIDA GPU获取GPU状态 一个Python模块,用于在Python中使用nvidia-smi以编程方式从NVIDA GPU获取GP ...

  10. Python多线程(3)——Queue模块

    Python多线程(3)--Queue模块 Queue模块支持先进先出(FIFO)队列,支持多线程的访问,包括一个主要的类型(Queue)和两个异常类(exception classes). Pyth ...

最新文章

  1. 『PaddlePaddle X Wechaty』有颜又有才的Living_Bot
  2. 互联网文本内容安全:腾讯云天御AI对抗实践
  3. webapi get请求 FromUri list参数传递
  4. 移动端中如何检测设备方向的变化?
  5. DirectShow 在VS2005中环境配置
  6. 【java】反射+poi 导出excel
  7. 运维转python开发_Python运维开发基础10-函数基础【转】
  8. Linux下yum配置及相关问题
  9. 用DIB位图显示图像
  10. Axure中SVG矢量图标的使用方法及资源推荐
  11. 西藏自治区林芝市谷歌高清卫星地图下载(百度网盘离线包下载)
  12. yigo项目中使用的函数
  13. 可以称为是“钢铁侠” 原型
  14. 机器学习【逻辑回归】——logistic函数逻辑回归鸢尾花分类
  15. R语言double数据转成numeric类型
  16. 利用计算机可以干什么,打开电脑不知道干什么 多个领域运用广【图解】
  17. 龙芯的全新自主指令集到底强在何处?
  18. xubuntu系统中设置上边框隐藏
  19. foobar2000 iOS使用,并连接PC的歌曲进行播放
  20. 关于《设计模式》与《设计模式沉思录》中提到的“常露齿嘻笑的猫”(Cheshire Cat)的说明...

热门文章

  1. 各种排序算法的实现直接插入排序
  2. 应用SqlParameter防止注入式攻击,打开主窗体关闭登录窗体
  3. vuforia for unity 注意事项
  4. 《图解算法》学习之算法复杂度、运行时间
  5. Kinect unity三维重建
  6. tinyxml读xml文件
  7. Atitit maven配置pom文件 配置法v2 t33 目录 1. Maven打包war原理 1 1.1. 在target目录生成war包ori目录。。。里面就是所有的资源 1 1.2. 去掉
  8. Atitit 中间件之道 attilax著 1. 第1章 中间件产生背景及分布式计算环境 2 2. 中间件分类 3 2.1. 商业中间件:weblogic,was,conherence 开源中间
  9. Atitit 分布式之道 attilax著 第4章 通信 第7章 一致性和复制 第8章 容错性 第9章 安全性 第10章 基于对象的分布式系统 第11章 分布式文件系统 第12章 基于Web的分
  10. Atitit sql执行计划