pandas的Series
pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)
首先介绍一下基本的:
data : array-like, dict, or scalar value,数组类型 index : array-like or Index (1d), dtype : numpy.dtype or None copy : boolean, default False 初始化时,如果只输入data和index,则得保证两者长度相同,否则报错:
>>> pd.Series(range(4),index=list("list"))
l 0
i 1
s 2
t 3
dtype: int32
>>> pd.Series(range(5),index=list("list"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "E:\Python3\lib\site-packages\pandas\core\series.py", line 245, in __init__
data = SingleBlockManager(data, index, fastpath=True)
File "E:\Python3\lib\site-packages\pandas\core\internals.py", line 4070, in __init__
fastpath=True)
File "E:\Python3\lib\site-packages\pandas\core\internals.py", line 2685, in make_block
return klass(values, ndim=ndim, fastpath=fastpath, placement=placement)
File "E:\Python3\lib\site-packages\pandas\core\internals.py", line 109, in __init__
len(self.mgr_locs)))
ValueError: Wrong number of items passed 5, placement implies 4
>>> pd.Series(range(4),index=list("lists"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "E:\Python3\lib\site-packages\pandas\core\series.py", line 245, in __init__
data = SingleBlockManager(data, index, fastpath=True)
File "E:\Python3\lib\site-packages\pandas\core\internals.py", line 4070, in __init__
fastpath=True)
File "E:\Python3\lib\site-packages\pandas\core\internals.py", line 2685, in make_block
return klass(values, ndim=ndim, fastpath=fastpath, placement=placement)
File "E:\Python3\lib\site-packages\pandas\core\internals.py", line 109, in __init__
len(self.mgr_locs)))
ValueError: Wrong number of items passed 4, placement implies 5
创建一个series:
>>> se = pd.Series(range(5))
>>> se.name = "values"
>>> se = pd.Series(range(5),name="values")
>>> se
0 0
1 1
2 2
3 3
4 4
Name: values, dtype: int32
# 两者效果等价
可以更改index:
>>> se.index
RangeIndex(start=0, stop=5, step=1) >>> se.index = list("abcde")
>>> se
a 0
b 1
c 2
d 3
e 4
Name: values, dtype: int32
将index列命名:
>>> se.index.name = "id"
>>> se
id
a 0
b 1
c 2
d 3
e 4
Name: values, dtype: int32
转化为dataframe:
>>> se.to_frame()
values
id
a 0
b 1
c 2
d 3
e 4
选出一个:
>>> se["b"]
1
>>> se.loc["b"]
1
但是里面的字符串不能用数字,(否则会被认为是切片操作选择):
>>> se[1] # 元素充足时
1 >>> se[5] # 元素不足时,报错
Traceback (most recent call last):
File "E:\Python3\lib\site-packages\pandas\indexes\base.py", line 2169, in get_value
tz=getattr(series.dtype, 'tz', None))
File "pandas\index.pyx", line 98, in pandas.index.IndexEngine.get_value (pandas\index.c:3557)
File "pandas\index.pyx", line 106, in pandas.index.IndexEngine.get_value (pandas\index.c:3240)
File "pandas\index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas\index.c:4279)
File "pandas\src\hashtable_class_helper.pxi", line 732, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13742)
File "pandas\src\hashtable_class_helper.pxi", line 740, in pandas.hashtable.PyObjectHashTable.get_item (pandas\hashtable.c:13696)
KeyError: 5 During handling of the above exception, another exception occurred: Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "E:\Python3\lib\site-packages\pandas\core\series.py", line 603, in __getitem__
result = self.index.get_value(self, key)
File "E:\Python3\lib\site-packages\pandas\indexes\base.py", line 2175, in get_value
return tslib.get_value_box(s, key)
File "pandas\tslib.pyx", line 946, in pandas.tslib.get_value_box (pandas\tslib.c:19053)
File "pandas\tslib.pyx", line 962, in pandas.tslib.get_value_box (pandas\tslib.c:18770)
IndexError: index out of bounds >>> se[5] = "s" # 也是错误的,越界了
pandas的Series的更多相关文章
- 利用Python进行数据分析(7) pandas基础: Series和DataFrame的简单介绍
一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...
- 金融量化分析【day110】:Pandas的Series对象
一.pandas简介安装 pandas是一个强大的python数据分析的工具包 pandsa是基于NumPy构建的 1.pandas的主要功能 1.具备对其功能的数据结构DataFrame.Serie ...
- Pandas之Series+DataFrame
Series是带有标签的一维数组,可以保存任何数据类型(整数,字符串,浮点数,python对象) index查看series索引,values查看series值 series相比于ndarray,是一 ...
- Python之Pandas中Series、DataFrame
Python之Pandas中Series.DataFrame实践 1. pandas的数据结构Series 1.1 Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一 ...
- 数据科学:Pandas 和 Series 的 describe() 方法
一.Pandas 和 Series 的 describe() 方法 1)功能 功能:对数据中每一列数进行统计分析:(以“列”为单位进行统计分析) 默认只先对“number”的列进行统计分析: 一列数据 ...
- Pandas 数据结构Series:基本概念及创建
Series:"一维数组" 1. 和一维数组的区别 # Series 数据结构 # Series 是带有标签的一维数组,可以保存任何数据类型(整数,字符串,浮点数,Python对象 ...
- Pandas之Series
# Series 数据结构 # Series 是带有标签的一维数组,可以保存任何数据类型(整数,字符串,浮点数,Python对象等),轴标签统称为索引 import numpy as np impor ...
- pandas学习series和dataframe基础
PANDAS 的使用 一.什么是pandas? 1.python Data Analysis Library 或pandas 是基于numpy的一种工具,该工具是为了解决数据分析人物而创建的. 2.p ...
- Python之Pandas中Series、DataFrame实践
Python之Pandas中Series.DataFrame实践 1. pandas的数据结构Series 1.1 Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一 ...
随机推荐
- 使用EasyNVR无插件流媒体服务器接口和EasyPlayer.js播放器插件实现web网页H5播放无插件
1.背景需求 很多客户在使用EasyNVR无插件流媒体服务器时,不喜欢产品化的界面,有时可能满足不了日常观看使用的需求.因此软件提供丰富的HTTP接口,供第三方平台调用集成.但是有时客户这边可能没有专 ...
- Python之可迭代对象、迭代器、生成器
在使用Python的过程中,很容易混淆如下几个关联的概念: 1.容器(container) 2.可迭代对象(Iterable) 3.迭代器(Iterator) 4.生成器(generator) 5.生 ...
- git github usage
以gerrit-trigger-plugin为例,下面的链接都是从相应页面上直接拷贝的. 法一:不用github的账号,打开这个库在github上的主页,运行下面命令即可 read only 运行命令 ...
- Delphi Pdf的使用方法
此方法安装了llPDFLib.v3.6 控件.对pdf左侧.右侧正文进行了操作. procedure TForm1.Button1Click(Sender: TObject); var node,nd ...
- Django 之 序列化
Django之序列化 关于Django中的序列化主要应用在将数据库中检索的数据返回给客户端用户,特别的Ajax请求一般返回的为Json格式. serializers 1 2 3 4 5 from dj ...
- Python操作Redis(一)
redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted set ...
- python requests 使用
快速上手 迫不及待了吗?本页内容为如何入门 Requests 提供了很好的指引.其假设你已经安装了 Requests.如果还没有,去安装一节看看吧. 首先,确认一下: Requests 已安装 Req ...
- spring 项目tomcat 8.0.2 发布报错:Could not initialize class org.hibernate.validator.engine.ConfigurationImpl
tomcat 8 项目发布遇到的错 [ERROR] -- ::, org.springframework.web.servlet.DispatcherServlet - Context initial ...
- Numpy用于数组的文件输入输出
这一章比较简单,内容也比较少.而且对于文件的读写,还是使用pandas比较好.numpy主要是读写文本数据和二进制数据的. 将数组以二进制的格式保存到硬盘上 主要的函数有numpy.save和nump ...
- 20145210姚思羽《网络对抗》MSF基础应用实验
20145210姚思羽<网络对抗>MSF基础应用实验 实验后回答问题 1.用自己的话解释什么是exploit,payload,encode. exploit就是进行攻击的那一步 paylo ...