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的更多相关文章

  1. 利用Python进行数据分析(7) pandas基础: Series和DataFrame的简单介绍

    一.pandas 是什么 pandas 是基于 NumPy 的一个 Python 数据分析包,主要目的是为了数据分析.它提供了大量高级的数据结构和对数据处理的方法. pandas 有两个主要的数据结构 ...

  2. 金融量化分析【day110】:Pandas的Series对象

    一.pandas简介安装 pandas是一个强大的python数据分析的工具包 pandsa是基于NumPy构建的 1.pandas的主要功能 1.具备对其功能的数据结构DataFrame.Serie ...

  3. Pandas之Series+DataFrame

    Series是带有标签的一维数组,可以保存任何数据类型(整数,字符串,浮点数,python对象) index查看series索引,values查看series值 series相比于ndarray,是一 ...

  4. Python之Pandas中Series、DataFrame

    Python之Pandas中Series.DataFrame实践 1. pandas的数据结构Series 1.1 Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一 ...

  5. 数据科学:Pandas 和 Series 的 describe() 方法

    一.Pandas 和 Series 的 describe() 方法 1)功能 功能:对数据中每一列数进行统计分析:(以“列”为单位进行统计分析) 默认只先对“number”的列进行统计分析: 一列数据 ...

  6. Pandas 数据结构Series:基本概念及创建

    Series:"一维数组" 1. 和一维数组的区别 # Series 数据结构 # Series 是带有标签的一维数组,可以保存任何数据类型(整数,字符串,浮点数,Python对象 ...

  7. Pandas之Series

    # Series 数据结构 # Series 是带有标签的一维数组,可以保存任何数据类型(整数,字符串,浮点数,Python对象等),轴标签统称为索引 import numpy as np impor ...

  8. pandas学习series和dataframe基础

    PANDAS 的使用 一.什么是pandas? 1.python Data Analysis Library 或pandas 是基于numpy的一种工具,该工具是为了解决数据分析人物而创建的. 2.p ...

  9. Python之Pandas中Series、DataFrame实践

    Python之Pandas中Series.DataFrame实践 1. pandas的数据结构Series 1.1 Series是一种类似于一维数组的对象,它由一组数据(各种NumPy数据类型)以及一 ...

随机推荐

  1. KVC && KVO 初见

    Look,这是一个很简单的要求,点击Add me,age +1. 想一想的话很简单的,设置一个属性Nsinteger age,点击button add me,直接加1在重新显示Lable就好啦,不过, ...

  2. Google 翻译如何获取 tk 参数值?

    1.首先获取 TKK 参数,这个参数可以在 https://translate.google.com 网页获取, src:TKK=eval('((function(){var a\x3d2089517 ...

  3. MySQL中备份的几种方式

    前言: 并不是每家公司都高大上,并不是每家公司都会用一些很前沿的技术来做备份这一块,有些企业或者有些行业或者团队本身由于各方面的原因使用简单或者复杂的方式来做备份这块,这次这个文档算是对以前工作的总结 ...

  4. Nginx学习(1)

    Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性: 作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 ...

  5. CentOS Linux中zip压缩和unzip解压缩命令详解

    以下命令均在/home目录下操作cd /home  #进入/home目录1.把/home目录下面的mydata目录压缩为mydata.zip     zip -r  mydata.zip   myda ...

  6. New Moto X 2014 全版本RSD&Fastboot刷官方底包教程

    本来我是不想写教程的,因为这样的教程实在是太多了,基本上大家也都会了,为什么还要多次一举,发来发去的呢?实在没什么意义!但是我觉得吧,别人的教程写的都太过简单,太过明了了,有时候我们很难理解,这到底是 ...

  7. LeetCode-day01&02

    感觉还好,坚持住就行,毕竟智商不够 1. Length of Last Word求一个数组的最后一个单词的长度 2. Plus One   大数加1 3.  Add Binary 二进制加法 4. S ...

  8. 算法题 18 像素翻转 牛客网 CC150

    算法题 18 像素翻转 牛客网 CC150_P114 题目描述 有一副由NxN矩阵表示的图像,这里每个像素用一个int表示,请编写一个算法,在不占用额外内存空间的情况下(即不使用缓存矩阵),将图像顺时 ...

  9. SpringMVC:学习笔记(3)——REST

    SpringMVC:学习笔记(3)——REST 了解REST风格 按照传统的开发方式,我们在实现CURD操作时,会写多个映射路径,比如对一本书的操作,我们会写多个URL,可能如下 web/delete ...

  10. CentOS7,将文本模式改成图形界面模式

    在以前通过vi /etc/inittab,将3修改成5.但是在centOS7之后将修改的办法换掉了,执行systemctl set-default graphical.target.根据提示进行一步一 ...