Pandas重塑和轴向旋转
重塑和轴向旋转
Se
import pandas as pd
import numpy as np
from pandas import Series
data=pd.DataFrame(np.arange(6).reshape(2,3),
index=['Ohio','Colorado'],
columns=['one','two','three']
)
data.index.names=['state']
data.columns.names=['number']
data
number one two three
state
Ohio 0 1 2
Colorado 3 4 5
# 使用该unstack()方法可将列转为行,一一对应,得到一个Series
result = data.stack()
result
state number
Ohio one 0
two 1
three 2
Colorado one 3
two 4
three 5
dtype: int32
# unstack()可以将其重排为一个DataFrame
result.unstack()
number one two three
state
Ohio 0 1 2
Colorado 3 4 5
# 默认情况下,unstack操作的是最里面的那层number,这个对象可以接收索引的编号啊或名称
result.unstack('state')
state Ohio Colorado
number
one 0 3
two 1 4
three 2 5
# 传入索引名称,都是讲state的层次化索引变为DataFrame格式
result.unstack(0)
state Ohio Colorado
number
one 0 3
two 1 4
three 2 5
# 当组里的值不是都有的时候,unstack会引入缺失数
s1 = Series([0,1,2,3],index=['a','b','c','d'])
s2 = Series([4,5,6], index=['c','d','e'])
data2 = pd.concat([s1,s2],keys=['one','two'])
data2
one a 0
b 1
c 2
d 3
two c 4
d 5
e 6
dtype: int64
data2.unstack()
a b c d e
one 0.0 1.0 2.0 3.0 NaN
two NaN NaN 4.0 5.0 6.0
# 但是stack却可以过滤掉缺失数据,如果不想过滤,可以dropna=False
data2.unstack().stack()
one a 0.0
b 1.0
c 2.0
d 3.0
two c 4.0
d 5.0
e 6.0
dtype: float64
# 这是不过滤的效果
data2.unstack().stack(dropna=False)
one a 0.0
b 1.0
c 2.0
d 3.0
e NaN
two a NaN
b NaN
c 4.0
d 5.0
e 6.0
dtype: float64
# DataFrame中的stack和unstack
result
state number
Ohio one 0
two 1
three 2
Colorado one 3
two 4
three 5
dtype: int32
df = pd.DataFrame({'left':result, 'right':result+5},columns=pd.Index(['left','right'],name='side'))
df
side left right
state number
Ohio one 0 5
two 1 6
three 2 7
Colorado one 3 8
two 4 9
three 5 10
# 对DataFrame进行unstack操作,会将旋转轴变为结果中的最低级别,变为层次化索引的最低级别
df.unstack('state')
side left right
state Ohio Colorado Ohio Colorado
number
one 0 3 5 8
two 1 4 6 9
three 2 5 7 10
# side也会是最低级别,把side折叠
df.unstack('state').stack('side')
state Colorado Ohio
number side
one left 3 0
right 8 5
two left 4 1
right 9 6
three left 5 2
right 10 7
时间序列数据的堆叠格式
data_c = [
['1959-03-31','realgdb',2710.349],
['1959-03-31','infl',0.000],
['1959-03-31','unemp',5.800],
['1959-06-30','realgdb',2778.801],
['1959-06-30','infl',2.340],
['1959-06-30','unemp',5.100],
['1959-09-30','realgdb',2775.488],
['1959-09-30','infl',2.740],
['1959-09-30','unemp',5.300],
]
ldata = pd.DataFrame(data_c,columns=['data','item','value'])
ldata
data item value
0 1959-03-31 realgdb 2710.349
1 1959-03-31 infl 0.000
2 1959-03-31 unemp 5.800
3 1959-06-30 realgdb 2778.801
4 1959-06-30 infl 2.340
5 1959-06-30 unemp 5.100
6 1959-09-30 realgdb 2775.488
7 1959-09-30 infl 2.740
8 1959-09-30 unemp 5.300
# 将data作为行索引,item作为列索引,最简单的方法,pivot快捷函数
ldata.pivot('data','item','value')
item infl realgdb unemp
data
1959-03-31 0.00 2710.349 5.8
1959-06-30 2.34 2778.801 5.1
1959-09-30 2.74 2775.488 5.3
# pivot其实是执行了如下两步,本质还是堆叠
#第一步
ldata.set_index(['data','item'])
value
data item
1959-03-31 realgdb 2710.349
infl 0.000
unemp 5.800
1959-06-30 realgdb 2778.801
infl 2.340
unemp 5.100
1959-09-30 realgdb 2775.488
infl 2.740
unemp 5.300
# 第二步
ldata.set_index(['data','item']).unstack()
value
item infl realgdb unemp
data
1959-03-31 0.00 2710.349 5.8
1959-06-30 2.34 2778.801 5.1
1959-09-30 2.74 2775.488 5.3
Pandas重塑和轴向旋转的更多相关文章
- pandas学习(创建多层索引、数据重塑与轴向旋转)
pandas学习(创建多层索引.数据重塑与轴向旋转) 目录 创建多层索引 数据重塑与轴向旋转 创建多层索引 隐式构造 Series 最常见的方法是给DataFrame构造函数的index参数传递两个或 ...
- pandas(八)重塑和轴向旋转
重塑层次化索引 层次化索引为DataFrame的重排提供了良好的一致性操作,主要方法有 stack :将数据的列旋转为行 unstack:将数据的行转换为列 用一个dataframe对象举例 In [ ...
- 利用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 ...
- Python Pandas 时间序列双轴折线图
时间序列pv-gmv双轴折线图 import numpy as np import pandas as pd import matplotlib.pyplot as plt n = 12 date_s ...
- pandas DataFrame(3)-轴
和numpy数组(5)-二维数组的轴一样,pandas DataFrame也有轴的概念,决定了方法是对行应用还是对列应用: 以下面这个数据为例说明: 这个数据是5个车站10天内的客流数据: rider ...
- CSS3.0动画之hover---Y轴----3D旋转
div#div2{display: table; width: 100%; height: 100%; text-decoration: none; outline: none; -webkit-tr ...
- 【学习】数据规整化:清理、转换、合并、重塑(续)【pandas】
@合并重叠数据 还有一种数据组合问题不能用简单的合并或连接运算来处理.比如说,你可能有索引全部或部分重叠的两个数据集 使用numpy的where函数,它用于表达一种矢量化的if - else a = ...
随机推荐
- jsp/servlet环境搭建
手动配置servlet开发环境: 1. eclipse.tomcat.jdk下载安装: 2. eclipse新建项目,项目依赖tomcat的jar包(包含tomcat和servlet相关jar包)以及 ...
- form表单以及内嵌框架标签
今关于今天所学习的东西又复杂又简单,上午学习了form表单,也是挺简单的:搭配table表格使用也是非常熟练. 下午讲了讲给网页内嵌框架标签以及在内嵌框架标签中添加其他的网页:还有在内嵌框架标签中添加 ...
- Cache架构设计
Cache策略 定时过期策略 定时过期的好处是Cache节点的个数符合实际需求,不会造成资源滥用和服务器压力 定时过期适合访问量较大,实时性要求不高的情况 如果访问量小,定时过期会造成Cache命中率 ...
- vue学习笔记——组件的优化
Vue 应用性能优化指南 1 给组件定义name,然后在同级目录新建index文件: import Count from './count.vue' export Count; 2 优化大数据的列表 ...
- PythonStudy——字典的定义 Dictionary definition
# 空字典 d1 = {} d2 = dict() # 用map映射创建字典 d3 = dict({'a': 1, 'b': 1}) print(d3) # 用关键字赋值方式 d4 = dict(na ...
- MySQL 批量添加
自己封装的一个批量添加. $data 是一个二维数组.key对应是数据表的字段名: /** * 批量创建 * @param array $data * @return int $res 影响行 * @ ...
- 【动态规划】最大连续子序列和,最大子矩阵和,最大m子段和
1.最大字段和问题 求一个序列最大连续子序列之和. 例如序列[-1,-2,-3,4,5,-6]的最大子段和为4 + 5 = 9. ①枚举法 int MaxSum(int n,int *a){ int ...
- js鼠标拖动(转载)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【rabbitmq】Centos7 下安装rabbitmq
rabbitmq安装 rabbitmq的安装依赖erlang,首先应该先安装erlang,然后安装rabbitmq: Step1:安装erlang erlang-rpm安装教程 选择在Centos7 ...
- react高阶组件
高阶组件 为了提高组件复用性,在react中就有了HOC(Higher-Order Component)的概念.所谓的高阶组件,其本质依旧是组件,只是它返回另外一个组件,产生新的组件可以对属性进行包装 ...