pandas-Series 笔记
来源:互联网 发布:深圳方维网络 编辑:程序博客网 时间:2024/05/22 03:29
Pandas-Series 笔记
- pandas 是建立在Numpy基础之上的
- 两大数据结构: 一纬: Series, 二维: DataFrame
Series数据类型: index + values
1. 生成
- pd.Series(可迭代对象list, tuple等) –> 默认index是 0, 1, 2…
- pd.Series(somevalues, index = 可迭代对象list, tuple. )
- pd.Series( adict ) –> 字典键值对被转化为 index value 对
- 可以指定dtype=类型
- 获得indexs和values
- pd_series.index
- pd_series.values
import pandas as pdimport numpy as nps1 = pd.Series()print(s1)
Series([], dtype: float64)
# 同时指定value 和 index的值s2 = pd.Series([1, 3, 5, 7, 9], index=['a', 'b', 'c', 'd', 'e'])s2
a 1b 3c 5d 7e 9dtype: int64
# 显示values2.values
array([1, 3, 5, 7, 9])
# 显示indexs2.index
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')
# series 中添加成员:有点s2['f'] = 11s2
a 1b 3c 5d 7e 9f 11dtype: int64
# 通过字典转化为 Seriess3 = pd.Series({'a': 4, 'b': 3, 'c': 2, 'd': 1})s3
a 4b 3c 2d 1dtype: int64
# 通过list仅仅指定 values , 说明默认的index是 123.。。s4 = pd.Series(range(10))s4
0 01 12 23 34 45 56 67 78 89 9dtype: int64
# 生成随机数的方法, 使用 numpy.random.randn(n)pd.Series(np.random.randn(5))
0 -1.8020351 -0.0720002 0.3932463 1.2377284 1.041300dtype: float64
2. 索引
- obj.head(n=5) # 默认查看对象前5 个数据
- obj.tail(n=5) # 默认查看对象后5 个数据
- obj.take(index) # 提取指定index标签位置的数据, 可以传递一个index 数组
- obj[i] # 获得指定 i索引位置 的数据
- obj[index] # 提取指定 index标签 位置的数据
s4.head() # 默认查看对象前5个
0 01 12 23 34 4dtype: int64
s4.head(3) # 查看前三个
0 01 12 2dtype: int64
s4.tail() # 查看后五个(默认)
5 56 67 78 89 9dtype: int64
sss = s4.take([3, 6, 8]) # take 可以传递一个数组print(ss)
1
s2
a 1b 3c 5d 7e 9f 11dtype: int64
# 注意注意!!! index修改为非数字序号以后, 仍然可以用位置索引进行提取和切片s2 = pd.Series([1, 3, 5, 7, 9], index=['a', 'b', 'c', 'd', 'e'])print(s2[1] is s2['b']) ###为什么是False, 我认为每次从内存中取值, 都重新创建了对象print(s2[1] is s2[1]) ###为什么是Falseprint(s2['c'] is s2[2])print(s2[1] == s2['b'])print(s2[[0, 1, 2]] == s2[['a', 'b', 'c']])# s2['b'] = 3s2
FalseFalseFalseTruea Trueb Truec Truedtype: boola 1b 3c 5d 7e 9dtype: int64
# 注意注意!!! index修改为非数字序号以后, 仍然可以用位置索引进行提取和切片s22 = pd.Series([1, 3, 5, 7, 9], index=['a', 'b', 'c', 'd', 'e'])s2['b'] = 'ccc'print(s2[1] is s2['b'])print(s2[1] is s2[1])print(s2['c'] is s2[2])print(s2[1] == s2['b'])print(s2[[0, 1, 2]] == s2[['a', 'b', 'c']])s2
TrueTrueTrueTruea Trueb Truec Truedtype: boola 1b cccc 5d 7e 9dtype: object
3. 切片
- 标签切片会包括 起始和 终止 两个位置的元素(因为通常不知道下一个标签是什么)
- 位置切片遵循Python切片规则
- 创建时没有指定index标签的, 标签==位置索引
- 当标签为数字, 但无规律的时候, 数字切片用的是位置索引
s4[0:3]
0 01 12 2dtype: int64
s5 = pd.Series(['a', 'b', 'c', 'd', 'e', 'f'], index=range(5,-1, -1))s5
5 a4 b3 c2 d1 e0 fdtype: object
# 现在呢? 现在索引会是用 标签, 还是位置?s5[:2]# 答案是用了 位置索引
5 a4 bdtype: object
s2['a':'c'] # 使用标签索引.
a 1b 3c 5dtype: int64
时间序列
- index取值为时间戳, 设置为Timestamp对象
- pd.Timestamp() 可以传入str类型, 也可以传入datatime类型的数据
- pd.to_datetime(atimelist)— 可以转化一个可迭代对象内元素为 Timestamp对象
- datetime对象可以直接作为index, pandas会自动将其转换为Timestamp对象
from datetime import datetimedate1 = datetime(2016, 1, 1)# 单个类型转换, 用 pd.Timestamp()date2 = pd.Timestamp(date1)date2
Timestamp('2016-01-01 00:00:00')
type(date2)
pandas._libs.tslib.Timestamp
# 序列转化为 Timestampts = pd.Series([1, 2, 3], index=pd.to_datetime([ '2017-08-09', '2017-08-10', '2017-08-11']))ts
2017-08-09 12017-08-10 22017-08-11 3dtype: int64
# 时间索引, 形式可以多样print(ts['20170809'])print('-'*15)print(ts['2017-08-09'])print('-'*15)print(ts['08/09/2017'])
1---------------1---------------1
# 时间索引:print(ts['20170810': '20170811'])
2017-08-10 22017-08-11 3dtype: int64
# 取某个时间**之前**的数据, 这个事件在index可以不存在ts.truncate(after='20170809')
2017-08-09 1dtype: int64
# 数据错位, # + 滞后, 也就是说, 同样的index,在这里拿到的数据是上一个index的, # - 超前, 默认不循环print(ts.shift(1))print('-'*30)print(ts.shift(-1))
2017-08-09 NaN2017-08-10 1.02017-08-11 2.0dtype: float64------------------------------2017-08-09 2.02017-08-10 3.02017-08-11 NaNdtype: float64
阅读全文
0 0
- pandas学习笔记-Series
- pandas-Series 笔记
- Pandas Series
- pandas: series
- Pandas Series
- pandas-Series
- Pandas-Series
- 我的python学习笔记(4) pandas 之 Series, DataFrame
- pandas-series总结
- python pandas series
- Pandas Series操作
- pandas数据结构Series学习
- pandas.Series函数用法
- python-pandas-series
- pandas.Series.asfreq
- pandas中的Series学习
- Pandas数据结构之:Series
- pandas-1:Series && DataFrame
- 二分查找的越界和死循环问题
- Kafka设计解析(四)- Kafka Consumer设计解析
- Dijkstra 算法 c语言原生实现
- 圣杯布局、双飞翼布局、Flex布局和绝对定位布局的几种经典布局的具体实现示例
- 二维数组中的查找
- pandas-Series 笔记
- PV操作--demo test
- 心跳协议的设计与实现
- Kafka设计解析(五)- Kafka性能测试方法及Benchmark报告
- 对服务器与客户端编码不同的解决方式2
- Linux查找含有某字符串的所有文件
- LeetCode Teemo Attacking
- Spring MVC Controller 入参类型
- C++笔试,面试题(一). 腾讯 & 多益网络