pandas.Series
1、系列(Series
)是能够保存任何类型的数据(整数,字符串,浮点数,Python对象等)的一维标记数组。轴标签统称为索引。
Pandas系列可以使用以下构造函数创建 -
pandas.Series( data, index, dtype, copy)。
编号 | 参数 | 描述 |
---|---|---|
1 | data |
数据采取各种形式,如:ndarray ,list ,constants |
2 | index |
索引值必须是唯一的和散列的,与数据的长度相同。 默认np.arange(n) 如果没有索引被传递。 |
3 | dtype |
dtype 用于数据类型。如果没有,将推断数据类型 |
4 | copy |
复制数据,默认为false 。 |
可以使用各种输入创建一个系列,如 数组、字典、标量值、或、常数
2、
import numpy as np
from scipy import linalg as lg
import pandas as pd
s = pd.Series()#创建一个基本系列是一个空系列
print(s)#Series([], dtype: float64) data = np.array(['a','b','c','d'])
s = pd.Series(data)#如果数据是ndarray,则传递的索引必须具有相同的长度。
# 如果没有传递索引值,那么默认的索引将是范围(n),其中n是数组长度
print (s)
# 0 a
# 1 b
# 2 c
# 3 d
# dtype: object data = np.array(['a','b','c','d'])
s = pd.Series(data,index=[100,101,102,103])#在这里传递了索引值。现在可以在输出中看到自定义的索引值
print (s)
# 100 a
# 101 b
# 102 c
# 103 d
# dtype: object data = {'a' : 0., 'b' : 1., 'c' : 2.}
s = pd.Series(data)#字典(dict)可以作为输入传递,如果没有指定索引,则按排序顺序取得字典键以构造索引。
# 如果传递了索引,索引中与标签对应的数据中的值将被拉出。
print (s)
# a 0.0
# b 1.0
# c 2.0
# dtype: float64 data = {'a' : 0., 'b' : 1., 'c' : 2.}
s = pd.Series(data,index=['b','c','d','a'])#注意观察 - 索引顺序保持不变,缺少的元素使用NaN(不是数字)填充
print (s)
# b 1.0
# c 2.0
# d NaN
# a 0.0
# dtype: float64 s = pd.Series(5, index=[0, 1, 2, 3])#如果数据是标量值,则必须提供索引。将重复该值以匹配索引的长度
print (s)
# 0 5
# 1 5
# 2 5
# 3 5
# dtype: int64 s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
#retrieve the first element
# 系列中的数据可以使用类似于访问ndarray中的数据来访问。
# 检索第一个元素。比如已经知道数组从零开始计数,第一个元素存储在零位置等等
print (s[0])# s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
#retrieve the first three element
# 检索系列中的前三个元素。 如果a:被插入到其前面,则将从该索引向前的所有项目被提取。
# 如果使用两个参数(使用它们之间),两个索引之间的项目(不包括停止索引)
print (s[:3])
# a 1
# b 2
# c 3
# dtype: int64 s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
#retrieve a single element
#一个系列就像一个固定大小的字典,可以通过索引标签获取和设置值。
print (s['a'])# s = pd.Series([1,2,3,4,5],index = ['a','b','c','d','e'])
#retrieve multiple elements 使用索引标签值列表检索多个元素。 如果不包含标签,则会出现异常。
print (s[['a','c','d']])
# a 1
# c 3
# d 4
# dtype: int64
pandas.Series的更多相关文章
- pandas Series的sort_values()方法
pandas Series的 sort_values() 方法能对Series进行排序,返回一个新的Series: s = pd.Series([np.nan, 1, 3, 10, 5]) 升序排列: ...
- pandas数组(pandas Series)-(5)apply方法自定义函数
有时候需要对 pandas Series 里的值进行一些操作,但是没有内置函数,这时候可以自己写一个函数,使用 pandas Series 的 apply 方法,可以对里面的每个值都调用这个函数,然后 ...
- pandas数组(pandas Series)-(4)NaN的处理
上一篇pandas数组(pandas Series)-(3)向量化运算里说到,将两个 pandas Series 进行向量化运算的时候,如果某个 key 索引只在其中一个 Series 里出现,计算的 ...
- pandas数组(pandas Series)-(3)向量化运算
这篇介绍下有index索引的pandas Series是如何进行向量化运算的: 1. index索引数组相同: s1 = pd.Series([1, 2, 3, 4], index=['a', 'b' ...
- pandas数组(pandas Series)-(2)
pandas Series 比 numpy array 要强大很多,体现在很多方面 首先, pandas Series 有一些方法,比如: describe 方法可以给出 Series 的一些分析数据 ...
- python. pandas(series,dataframe,index) method test
python. pandas(series,dataframe,index,reindex,csv file read and write) method test import pandas as ...
- Python Pandas -- Series
pandas.Series class pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath ...
- pandas.Series.value_counts
pandas.Series.value_counts Series.value_counts(normalize=False, sort=True, ascending=False, bins=Non ...
- pandas.Series函数用法
class pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False) e.g., ...
随机推荐
- go标准库的学习-crypto/md5
参考:https://studygolang.com/pkgdoc 导入方式: import "crypto/md5" md5包实现了MD5哈希算法,参见RFC 1321. Con ...
- 如何在自己设计的页面中调用metamask-1
启发: https://github.com/MetaMask/metamask-extension/issues/714 https://github.com/MetaMask/metamask-e ...
- Linux下 XordDos(BillGates)木马查杀记录
最近朋友的一台服务器突然网络异常,cpu占用率暴表,登录上去一查,cpu占用300% 左右,流量异常,经过看查进程,获取信息最终确认为中了dos木马,经过几天的研究,基本上已经清除,以下是清理记录. ...
- Spark学习之JavaRdd
RDD 介绍 RDD,全称Resilient Distributed Datasets(弹性分布式数据集),是Spark最为核心的概念,是Spark对数据的抽象.RDD是分布式的元素集合,每个RDD只 ...
- Python-2.7 : 编码问题及encode与decode
普通的字符串在py2.7中都是以ASCII编码的,例如str=“abc”,若含有中文则会以gbk或者gb2312编码(GB2312是中国规定的汉字编码,也可以说是简体中文的字符集编码;GBK 是 GB ...
- keystone系列一:keystone基础
一 什么是keystone keystone是OpenStack的身份服务,暂且可以理解为一个'与权限有关'的组件. 二 为何要有keystone Keystone项目的主要目的是为访问opensta ...
- Oracle 在函数或存储过程中执行sql查询字符串并将结果值赋值给变量
请看黄色部分 --区县指标 THEN TVALUE_SQL := 'SELECT TO_CHAR(' || CUR_ROW.MAIN_FIELD || ') FROM ' || CUR_ROW.END ...
- 推荐几本对于Python初学者比较好的书籍(内含PDF)
我们提供一个初学者最好的Python书籍列表.Python是一个初级程序员可以学习编程的最友好语言之一.为了帮助您开始使用Python编程,我们分享此列表.泡一杯茶,选一本书阅读,开始使用Python ...
- 搭建mysql cluster
虚拟机搭建Mysql Cluster 参考文档:http://www.cnblogs.com/jackluo/archive/2013/01/19/2868152.html http://www.cn ...
- SQL Server中删除用户时报错,提示:The database principal owns a schema in the database, and cannot be dropped(转载)
Description of the problem: When you tried to drop a user, you got this message: Error: 15138 The da ...