二、pandas系列---pandas库的Series类型
来源:互联网 发布:淘宝里美即官方旗舰店 编辑:程序博客网 时间:2024/06/06 06:41
pandas系列—pandas库的Series类型
- pandas系列pandas库的Series类型
- 一Series概述
- 二创建Series类型的方法
- 1 从标量值创建
- 2 从字典类型创建
- 3 从ndarray类型创建
- 三Series类型的基本操作
- 1 简单的操作
- 3 Series类似ndarray类型的操作
- 3 Series类似python字典类型的操作
- 4 Series类型的对齐操作
- 5 Series类型的name属性
- 6 Series类型的修改
一、Series概述
Series是由一组数据及与之相关的数据索引组成
例子:
import pandas as pda = pd.Series([9,8,7,6]) #自动索引print(a)
运行结果:
0 9
1 8
2 7
3 6
dtype: int64
import pandas as pdb = pd.Series([9,8,7,6],index=['a','b','c','d']) #自定义索引print(b)
运行结果:
a 9
b 8
c 7
d 6
dtype: int64
二、创建Series类型的方法
Series类型可由如下类型创建:python列表、标量值、python字典、ndarray、其他函数
2.1 从标量值创建
import pandas as pda = pd.Series(25,index=['a','b','c','d'])print(a)
运行结果:
a 25
b 25
c 25
d 25
dtype: int64
2.2 从字典类型创建
例子1:
import pandas as pdb = pd.Series({'a':9,'b':8,'c':7})print(b)
运行结果:
a 9
b 8
c 7
dtype: int64
例子2:
import pandas as pdc = pd.Series({'a':9,'b':8,'c':7},index=['c','a','b','d'])print(c)
运行结果:
c 7.0
a 9.0
b 8.0
d NaN
dtype: float64
2.3 从ndarray类型创建
import pandas as pdimport numpy as npd = pd.Series(np.arange(5))e = pd.Series(np.arange(5),index = np.arange(9,4,-1))print(d)print(e)
运行结果:
0 0
1 1
2 2
3 3
4 4
dtype: int32
9 0
8 1
7 2
6 3
5 4
dtype: int32
三、Series类型的基本操作
3.1 简单的操作
Series类型包括index和values两部分
import pandas as pdg = pd.Series([9,8,7,6],['a','b','c','d'])gOut[16]: a 9b 8c 7d 6g.indexOut[18]: Index(['a', 'b', 'c', 'd'], dtype='object')g.valuesOut[19]: array([9, 8, 7, 6], dtype=int64)g['b']Out[20]: 8g[1]Out[21]: 8dtype: int64g[['c','d',0]] #自定义索引和自动索引不能混合使用Out[24]: c 7.0d 6.00 NaNdtype: float64g[['c','d','a']]Out[25]: c 7d 6a 9dtype: int64
3.3 Series类似ndarray类型的操作
import pandas as pdm = pd.Series([9,8,7,6],['a','b','c','d'])mOut[32]: a 9b 8c 7d 6dtype: int64m[3]Out[33]: 6m[:3]Out[34]: a 9b 8c 7dtype: int64m[m>m.median()]Out[35]: a 9b 8dtype: int64np.exp(m)Out[36]: a 8103.083928b 2980.957987c 1096.633158d 403.428793dtype: float64
3.3 Series类似python字典类型的操作
import pandas as pdh = pd.Series([9,8,7,6],['a','b','c','d'])h['b']Out[27]: 8'c' in h #保留字in只会判断自定义索引Out[28]: True0 in hOut[29]: Falseb.get('f',100) #若存在,就返回‘f’对应的值,若不存在,就返回100Out[30]: 100
3.4 Series类型的对齐操作
Series在运算中回自动对齐不同索引的数据
import pandas as pdp = pd.Series([9,8,7,6],['a','b','c','d'])q = pd.Series([1,2,3],['c','b','e'])p+qOut[39]: a NaNb 10.0c 8.0d NaNe NaNdtype: float64
3.5 Series类型的name属性
Series对象和索引都可以都一个名字,存储在属性.name中
import pandas as pds = pd.Series([9,8,7,6],['a','b','c','d'])sOut[42]: a 9b 8c 7d 6dtype: int64s.names.name='Series对象's.index.name='索引列'sOut[46]: 索引列a 9b 8c 7d 6Name: Series对象, dtype: int64
3.6 Series类型的修改
import pandas as pdt = pd.Series([9,8,7,6],['a','b','c','d'])t['a']=15t.name = 'Series'tOut[50]: a 15b 8c 7d 6Name: Series, dtype: int64t.name='New Series't['b','c']=20tOut[53]: a 15b 20c 20d 6Name: New Series, dtype: int64
- 注1:Series是一维带标签数组
- 注2:Series基本操作类似ndarray和字典,根据索引对齐
参考资料:北京理工大学嵩天老师教学视频
阅读全文
0 0
- 二、pandas系列---pandas库的Series类型
- Pandas库的使用--Series
- 三、pandas系列---pandas库的DataFrame类型
- Pandas Series
- pandas: series
- Pandas Series
- pandas-Series
- Pandas-Series
- pandas的数据结构-Series
- 四、pandas系列---pandas库的数据类型操作
- 五、pandas系列---pandas库的数据类型运算
- python pandas库的学习笔记二pandas的基本功能
- 一、pandas系列---pandas库简介
- pandas 中Series的map函数
- pandas学习:series的基本用法
- pandas Series 的索引对象(一)
- pandas的数据类型之Series,DataFrame
- 使用Pandas的Series方法绘制图像
- 【转】ACM高精度模板
- 通过ping、traceroute、tcpdump困扰已久的网络问题终于解决了
- 一行代码解决多渠道打包
- Git 常用命令详解(二)
- 快速排序
- 二、pandas系列---pandas库的Series类型
- vimの学习笔记 删除方法
- JQuery之动画
- CodeForces144A Arrival of the General
- JMeter-后置处理器
- TP3.2数据库转换
- CountDownLatch
- MySQL 第八天(核心优化二)
- CCF CSP认证 201512-2 消除类游戏 java版 70分,求助!