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数据类型)以及一 ...
随机推荐
- Building Shops
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Submissi ...
- EasyGBS国标流媒体服务器GB28181国标方案安装使用文档
EasyGBS - GB28181 国标方案安装使用文档 下载 安装包下载,正式使用需商业授权, 功能一致 在线演示 在线API 架构图 EasySIPCMS SIP 中心信令服务, 单节点, 自带一 ...
- 技术总结PHP+微信
ajax: //获取商品属性数据 function initGoodsAttr(){ $.ajax({ type: 'GET', url:"<?php ...
- TreeMap详细介绍(源码解析)和使用示例
本文转自 http://www.cnblogs.com/skywang12345/p/3310928.html 概要 这一章,我们对TreeMap进行学习.我们先对TreeMap有个整体认识,然后再学 ...
- Django的admin样式丢失【静态文件收集】
在部署完Django项目后,进行admin后台登录发现样式丢失,后台日志显示:js和css文件丢失 解决办法: 配置settings.py如下: #DEBUG打开时,app的静态文件默认从这里读取 S ...
- 让linux进程后台运行、会话断开不退出
方法一:nohup ping www.ibm.com & 可改变进程的父进程号方法二:setsid ping www.ibm.com 可改变进程的父进程号方法三:(ping www.ibm.c ...
- JDBC请求
做JDBC请求,首先需要两个jar包:mysql驱动-mysql-connector-java-5.1.13-bin.jar 和 sqlServer驱动-sqljdbc4.jar,将这两个jar包放到 ...
- corethink功能模块探索开发(四)让这个模块跑起来
让这个模块跑起来,太费劲了,多半原因是自己太粗心,opencmf.php中“uid”写成了“pid”,de了好几天的bug也没有搞出来,又加上最近发生了些事(brokenhearted)... 上报错 ...
- JAVA抠取Excel中的图片
EXCEL中扔了一堆的图片,老大让对应到数据库中的数据上.思路先把图片抠出存成单个图片.然后上传到服务器,取下路径更新到数据库中. 注释掉的部分为有多个Excel时使用. package com.** ...
- PAT 天梯赛 L1-029. 是不是太胖了 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-029 AC代码 #include <iostream> #include <cstdio&g ...