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数据类型)以及一 ...
随机推荐
- 学习 Unix 常用命令
第一个是 man 命令,作用是:"Display system documentation",我是 manual 的缩写.通过这个命令,我们能了解接下来要学习的命令的文档. ls, ...
- Nuxt使用scss
Nuxt中使用scss也很简单,分简单的几步就OK 一.安装scss依赖 用IDE打开项目,在Terminal里通过 npm i node-sass sass-loader scss-loader - ...
- Linux中的欢迎信息
本地终端欢迎信息 /etc/issue \d 显示当前系统日期 \s 显示操作系统名称 \l 显示终端的终端号,这个比较常用 \m 显示硬件体系结构,如i386.i68 ...
- sqlserver整理的实用资料
1 --- 创建 备份数据的 device 2 3 USE DB_ZJ 4 EXEC sp_addumpdevice 'disk', 'testBack', 'c:\MyNwind_1.dat' 5 ...
- LeetCode-day03
28. Best Time to Buy and Sell Stock 买卖股票的最好时间 29. Best Time to Buy and Sell Stock II 买卖股票2(多次买入,一次卖出 ...
- 标准c时间与日期函数
标准c时间与日期函数 asctime 语法: #include <time.h> char *asctime( const struct tm *ptr ); 功能: 函数将p ...
- 【整理学习Hadoop】H D F S 一个分布式文件系统
Hadoop分布式文件系统(HDFS)被设计成适合运行在通用硬件(commodity hardware)上的分布式文件系统.它和现有的分布式文件系统有很多共同点.但同时,它和其他的分布式文件系统的区别 ...
- 谷歌机器学习速成课程---2深入了解机器学习(Descending into ML)
1.线性回归 人们早就知晓,相比凉爽的天气,蟋蟀在较为炎热的天气里鸣叫更为频繁.数十年来,专业和业余昆虫学者已将每分钟的鸣叫声和温度方面的数据编入目录.Ruth 阿姨将她喜爱的蟋蟀数据库作为生日礼物送 ...
- Nexus Repository Manager 使用笔记
在使用maven是,因内外网限制往往需要配置自由的maven库,小编看见网上教程数不胜数,遍主动试试 以下是下载地址 : http://www.sonatype.com/download-oss-so ...
- PAT 天梯赛 L1-031. 到底是不是太胖了 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-031 AC代码 #include <iostream> #include <cstdio&g ...