pandas(八)重塑和轴向旋转
重塑层次化索引
层次化索引为DataFrame的重排提供了良好的一致性操作,主要方法有
stack :将数据的列旋转为行
unstack:将数据的行转换为列
用一个dataframe对象举例
In [4]: data = DataFrame(np.arange(6).reshape((2,3)),index = pd.Index(['Ohio','Colorado'],name='state'),columns = pd.Index(['one','two','three'],name = 'number')) In [5]: data
Out[5]:
number one two three
state
Ohio 0 1 2
Colorado 3 4 5 In [6]: data.stack()#将列索引转换为行索引
Out[6]:
state number
Ohio one 0
two 1
three 2
Colorado one 3
two 4
three 5
dtype: int32 In [7]: data.unstack()#将行索引转换为列索引
Out[7]:
number state
one Ohio 0
Colorado 3
two Ohio 1
Colorado 4
three Ohio 2
Colorado 5
dtype: int32 In [9]: data.unstack().index
Out[9]:
MultiIndex(levels=[['one', 'two', 'three'], ['Ohio', 'Colorado']],
labels=[[0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1]],
names=['number', 'state']) In [10]:
对于DataFrame,无论是使用unstack,还是stack,得到都是一个Series对象
Series对象,只有unstack方法。
默认情况下,unstack操作的是最内层,传入分层级别的编号或名称即可对相应级别的索引做操作。
In [21]: result.unstack(0)
Out[21]:
state Ohio Colorado
number
one 0 3
two 1 4
three 2 5 In [22]: result.unstack()
Out[22]:
number one two three
state
Ohio 0 1 2
Colorado 3 4 5 In [23]: result.unstack('state')
Out[23]:
state Ohio Colorado
number
one 0 3
two 1 4
three 2 5
如果不是所有的级别的值都能在个分组中找到的话,则unstack会引入缺失值
In [24]: s1 =Series([0,1,2,3],index = ['a','b','c','d']) In [25]: s2 = Series([4,5,6],index = ['c','d','e']) In [26]: data2 = pd.concat([s1,s2],keys = ['one','two']) In [27]: data2
Out[27]:
one a 0
b 1
c 2
d 3
two c 4
d 5
e 6
dtype: int64 In [28]: data2.unstack()
Out[28]:
a b c d e
one 0.0 1.0 2.0 3.0 NaN
two NaN NaN 4.0 5.0 6.0 In [29]: data2.unstack(0)
Out[29]:
one two
a 0.0 NaN
b 1.0 NaN
c 2.0 4.0
d 3.0 5.0
e NaN 6.0
而stack默认会滤除缺失值。
在对DataFrame进行旋转操作时,旋转的轴会成为旋转后索引的最低级别。也就是最内层索引。
pandas(八)重塑和轴向旋转的更多相关文章
- Pandas重塑和轴向旋转
重塑和轴向旋转 Se import pandas as pd import numpy as np from pandas import Series data=pd.DataFrame(np.ara ...
- pandas学习(创建多层索引、数据重塑与轴向旋转)
pandas学习(创建多层索引.数据重塑与轴向旋转) 目录 创建多层索引 数据重塑与轴向旋转 创建多层索引 隐式构造 Series 最常见的方法是给DataFrame构造函数的index参数传递两个或 ...
- 利用Python进行数据分析(13) pandas基础: 数据重塑/轴向旋转
重塑定义 重塑指的是将数据重新排列,也叫轴向旋转. DataFrame提供了两个方法: stack: 将数据的列“旋转”为行. unstack:将数据的行“旋转”为列. 例如: 处理堆叠格式 ...
- WPF动画旋转(3轴同时旋转问题)
原文:WPF动画旋转(3轴同时旋转问题) WPF的资料比较少,做起来不是很方便,之前一直有个XYZ3个轴同时旋转的问题,开始的时候以为通过 this.theRotateX.Axis = new Ve ...
- 【OSG细节实现】节点围绕位于axisPos平行于axis的轴进行旋转
//绕着与axis平行的任意轴旋转 void rotate(const std::string& name, float angle, osg::Vec3 axisPos, osg::Vec3 ...
- CSS3.0动画之hover---Y轴----3D旋转
div#div2{display: table; width: 100%; height: 100%; text-decoration: none; outline: none; -webkit-tr ...
- IOS7学习之路八(iOS 禁止屏幕旋转的方法)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { retu ...
- 【学习】数据规整化:清理、转换、合并、重塑(续)【pandas】
@合并重叠数据 还有一种数据组合问题不能用简单的合并或连接运算来处理.比如说,你可能有索引全部或部分重叠的两个数据集 使用numpy的where函数,它用于表达一种矢量化的if - else a = ...
- Python 数据分析(一) 本实验将学习 pandas 基础,数据加载、存储与文件格式,数据规整化,绘图和可视化的知识
第1节 pandas 回顾 第2节 读写文本格式的数据 第3节 使用 HTML 和 Web API 第4节 使用数据库 第5节 合并数据集 第6节 重塑和轴向旋转 第7节 数据转换 第8节 字符串操作 ...
随机推荐
- Error: EACCES: permission denied, symlink
环境说明 ganiks@ganiks-ubuntu-trusty-64:/ganiks/parse-server$ npm -v 6.5.0 ganiks@ganiks-ubuntu-trusty-6 ...
- 简单而直接的Python web 框架:web.py
web.py 是一个Python 的web 框架,它简单而且功能强大.web.py 是公开的,无论用于什么用途都是没有限制的. 先让大家感受一下web.py 的简单而强大: import web ur ...
- springBoot文档地址
文档: https://www.gitbook.com/book/qbgbook/spring-boot-reference-guide-zh/details 配置: http://docs.spri ...
- 'Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set.
UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATA ...
- 打开.py文件的方法
用IDLE打开这个文件,然后按F5,系统就自动开始运行这个python程序,然后当前运行目录就跳转到这个目录了
- [ppurl]从”皮皮书屋”下载电子书的姿势
(欢迎转载,转载请注明出处:http://blog.csdn.net/hcbbt/article/details/42072545) 写在前面的扯皮 为什么标题的"皮皮书屋"加上了 ...
- poj 2187:Beauty Contest(计算几何,求凸包,最远点对)
Beauty Contest Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 26180 Accepted: 8081 D ...
- Eclipse Android 代码自己主动提示功能
Eclipse Android 代码自己主动提示功能 Eclipse for android 实现代码自己主动提示智能提示功能.介绍 Eclipse for android 编辑器中实现两种主要文件 ...
- SqlBulkCopy 通过泛型数组批量插入
public void SqlBulkCopy<T>(string tablename, List<T> list) { Type recordType = typeof(T) ...
- [Algorithms] Sorting Algorithms (Insertion Sort, Bubble Sort, Merge Sort and Quicksort)
Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merg ...