Python Pandas -- Series
pandas.Series
class pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)
One-dimensional ndarray with axis labels (including time series).
Labels need not be unique but must be any hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. Statistical methods from ndarray have been overridden to automatically exclude missing data (currently represented as NaN)
Operations between Series (+, -, /, , *) align values based on their associated index values– they need not be the same length. The result index will be the sorted union of the two indexes.
Parameters : |
data : array-like, dict, or scalar value
index : array-like or Index (1d)
dtype : numpy.dtype or None
copy : boolean, default False
|
---|
Series 类似数组,但是它有标签(label) 或者索引(index).
1. 从最简单的series开始看。
from pandas import Series, DataFrame
import pandas as pd
ser1 = Series([1,2,3,4])
print(ser1)
#0 1
#1 2
#2 3
#3 4
#dtype: int64
此时因为没有设置index,所以用默认
2. 加上索引
ser2 = Series(range(4),index=['a','b','c','d'])
print(ser2)
#a 0
#b 1
#c 2
#d 3
#dtype: int64
3. dictionnary 作为输入
dict1 = {'ohio':35000,'Texas':71000,'Oregon':1600,'Utah':500}
ser3 = Series(dict1)
#Oregon 1600
#Texas 71000
#Utah 500
#ohio 35000
#dtype: int64
key:默认设置为index
dict1 = {'ohio':35000,'Texas':71000,'Oregon':1600,'Utah':500}
ser3 = Series(dict1)
#Oregon 1600
#Texas 71000
#Utah 500
#ohio 35000
#dtype: int64
print(ser3)
states = ['California', 'Ohio', 'Oregon', 'Texas']
ser4 = Series(dict1,index = states)
print(ser4)
#California NaN
#Ohio NaN
#Oregon 1600.0
#Texas 71000.0
#dtype: float64
用了dictionary时候,也是可以特定的制定index的,当没有map到value的时候,给NaN.
print(pd.isnull(ser4))
#California True
#Ohio True
#Oregon False
#Texas False
#dtype: bool
函数isnull判断是否为null
print(pd.isnull(ser4))
#California True
#Ohio True
#Oregon False
#Texas False
#dtype: bool
函数notnull判断是否为非null
print(pd.notnull(ser4))
#California False
#Ohio False
#Oregon True
#Texas True
#dtype: bool
4. 访问元素和索引用法
print (ser2['a']) #
#print (ser2['a','c']) error
print (ser2[['a','c']])
#a 0
#c 2
#dtype: int64
print(ser2.values) #[0 1 2 3]
print(ser2.index) #Index(['a', 'b', 'c', 'd'], dtype='object')
5. 运算, pandas的series保留Numpy的数组操作
print(ser2[ser2>2])
#d 3
#dtype: int64
print(ser2*2)
#a 0
#b 2
#c 4
#d 6
#dtype: int64
print(np.exp(ser2))
#a 1.000000
#b 2.718282
#c 7.389056
#d 20.085537
#dtype: float64
6. series 的自动匹配,这个有点类似sql中的full join,会基于索引键链接,没有的设置为null
print (ser3+ser4)
#California NaN
#Ohio NaN
#Oregon 3200.0
#Texas 142000.0
#Utah NaN
#ohio NaN
#dtype: float64
7. series对象和索引都有一个name属性
ser4.index.name = 'state'
ser4.name = 'population count'
print(ser4)
#state
#California NaN
#Ohio NaN
#Oregon 1600.0
#Texas 71000.0
#Name: population count, dtype: float64
8.预览数据
print(ser4.head(2))
print(ser4.tail(2))
#state
#California NaN
#Ohio NaN
#Name: population count, dtype: float64
#state
#Oregon 1600.0
#Texas 71000.0
#Name: population count, dtype: float64
Python Pandas -- 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&&DataFrame&& set_index&reset_index
参考CookBook :http://pandas.pydata.org/pandas-docs/stable/cookbook.html Pandas set_index&reset_ind ...
- python pandas ---Series,DataFrame 创建方法,操作运算操作(赋值,sort,get,del,pop,insert,+,-,*,/)
pandas 是基于 Numpy 构建的含有更高级数据结构和工具的数据分析包 pandas 也是围绕着 Series 和 DataFrame 两个核心数据结构展开的, 导入如下: from panda ...
- Python pandas 0.19.1 Intro to Data Structures 数据结构介绍 文档翻译
官方文档链接http://pandas.pydata.org/pandas-docs/stable/dsintro.html 数据结构介绍 我们将以一个快速的.非全面的pandas的基础数据结构概述来 ...
- Python pandas学习总结
本来打算学习pandas模块,并写一个博客记录一下自己的学习,但是不知道怎么了,最近好像有点急功近利,就想把别人的东西复制过来,当心沉下来,自己自觉地将原本写满的pandas学习笔记删除了,这次打算写 ...
- pandas.Series
1.系列(Series)是能够保存任何类型的数据(整数,字符串,浮点数,Python对象等)的一维标记数组.轴标签统称为索引. Pandas系列可以使用以下构造函数创建 - pandas.Series ...
- Python pandas快速入门
Python pandas快速入门2017年03月14日 17:17:52 青盏 阅读数:14292 标签: python numpy 数据分析 更多 个人分类: machine learning 来 ...
- Python pandas & numpy 笔记
记性不好,多记录些常用的东西,真·持续更新中::先列出一些常用的网址: 参考了的 莫烦python pandas DOC numpy DOC matplotlib 常用 习惯上我们如此导入: impo ...
- 【跟着stackoverflow学Pandas】 - Adding new column to existing DataFrame in Python pandas - Pandas 添加列
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
随机推荐
- 【Boost】boost库中timer定时器 2
博客转载自:http://blog.csdn.net/yockie/article/details/40386145 先跟着boost文档中asio章节的指南中的几个例子学习一下使用: 所有的Asio ...
- UltraISO制作系统ISO镜像
一.简介 UltraISO是一款功能强大而又方便实用的光盘映像文件制作/编辑/转换工具,它可以直接编辑ISO文件和从ISO中提取文件和目录,也可以从CD-ROM制作光盘映像或者将硬盘上的文件制作成IS ...
- latex中的空格
两个quad空格 a \qquad b 两个m的宽度 quad空格 a \quad b 一个m的宽度 大空格 a\ b 1/3m宽度 中等空格 a\;b 2/7m宽度 小空格 a\,b 1/6m宽度 ...
- SDUT 3343 数据结构实验之二叉树四:还原二叉树
数据结构实验之二叉树四:还原二叉树 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 给定一棵 ...
- LeetCode第136题:只出现一次的数字
问题描述 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? 示例 1: ...
- C++面试笔记--STL模板与容器
1.C++ STL 之所以得到广泛的赞誉,也被很多人使用,不只是提供了像vector, string, list等方便的容器,更重要的是STL封装了许多复杂的数据结构算法和大量常用数据结构操作.vec ...
- TCP/IP与套接字
以前我们讲过进程间通信,通过进程间通信可以实现同一台计算机上不同的进程之间通信. 通过网络编程可以实现在网络中的各个计算机之间的通信. 进程能够使用套接字实现和其他进程或者其他计算机通信. 同样的套接 ...
- SLAM(Linux版)
之前的那个是Windows版,现在终于发现Windows运行slam是不行的,多么痛的领悟. 本书系统地介绍了视觉SLAM所需的基本知识与核心算法,既包括数学理论基础,如三维空间的刚体运动.非线性优化 ...
- Adb无法连接Genymotion
后来注意到Genymotion启动的时候adb无法正常使用,反复调用adb启动新的server也无法解决 解决起来很简单.打开Genymotion设置界面,进入ADB标签,选中Use custom A ...
- VTK-py读取与显示相关函数
在VTK中可视化数据的一般流程如下: 文件输入相关函数: https://www.vtk.org/Wiki/VTK/Examples/Cxx#Input_and_Output Readers ● Re ...