Understanding the Transform Function in Pandas
Understanding the Transform Function in Pandas
What is transform?
我在 Python Data Science Handbook 的书中找到了关于这个话题的说明。正如书中所描述的,transform 是一个和groupby同时使用的操作。我推测大多数的pandas用户可能已经用过了aggregate, filter 或者 apply在使用 groupby的同时。然而,transform 有点难以理解。
aggregation会返回数据的缩减版本,而transformation能返回完整数据的某一变换版本供我们重组。这样的transformation,输出的形状和输入一致。一个常见的例子是通过减去分组平均值来居中数据。
First Approach - Merging
data_str='''account,name,order,sku,quantity,unit price,ext price
383080,Will LLC,10001,B1-20000,7,33.69,235.83
383080,Will LLC,10001,S1-27722,11,21.12,232.32
383080,Will LLC,10001,B1-86481,3,35.99,107.97
412290,Jerde-Hilpert,10005,S1-06532,48,55.82,2679.36
412290,Jerde-Hilpert,10005,S1-82801,21,13.62,286.02
412290,Jerde-Hilpert,10005,S1-06532,9,92.55,832.95
412290,Jerde-Hilpert,10005,S1-47412,44,78.91,3472.04
412290,Jerde-Hilpert,10005,S1-27722,36,25.42,915.12
218895,Kulas Inc,10006,S1-27722,32,95.66,3061.12
218895,Kulas Inc,10006,B1-33087,23,22.55,518.65
218895,Kulas Inc,10006,B1-33364,3,72.3,216.9
218895,Kulas Inc,10006,B1-20000,-1,72.18,-72.18'''
import io
import pandas as pd
data=pd.read_csv(io.StringIO(data_str))
order_total = data.groupby('order')['ext price'].sum().rename('order total').reset_index()
data_merge=data.merge(order_total)
data_merge['Percnet Order']=data_merge['ext price']/data_merge['order total']

what is happening with the standard groupby
Second Approach - Using Transform
order_total=data.groupby('order')['ext price'].transform('sum')
data['percent order'] = data['ext price']/order_total

what is happening in transform
Understanding the Transform Function in Pandas的更多相关文章
- Understanding JavaScript Function Invocation and "this"
Understanding JavaScript Function Invocation and "this" 11 Aug 2011 Over the years, I've s ...
- Pandas进阶笔记 (一) Groupby 重难点总结
如果Pandas只是能把一些数据变成 dataframe 这样优美的格式,那么Pandas绝不会成为叱咤风云的数据分析中心组件.因为在数据分析过程中,描述数据是通过一些列的统计指标实现的,分析结果也需 ...
- pandas 之 特征工程
import numpy as np import pandas as pd So far(到目前为止) in this chapter we've been concerned with rearr ...
- 深入node之Transform
Transform流特性 在开发中直接接触Transform流的情况不是很多,往往是使用相对成熟的模块或者封装的API来完成流的处理,最为特殊的莫过于through2模块和gulp流操作.那么,Tra ...
- CSS3 Transform——transform-origin
2012年9月,W3C组织发布了CSS3变形工作草案.CSS3变形允许CSS把元素转变为2D 或3D空间,这个草案包括了CSS3 2D变形和CSS3 3D变形. CSS3变形是一些效果的集合,比如平移 ...
- 数据处理:12个使得效率倍增的pandas技巧
数据处理:12个使得效率倍增的pandas技巧 1. 背景描述 Python正迅速成为数据科学家偏爱的语言,这合情合理.它拥有作为一种编程语言广阔的生态环境以及众多优秀的科学计算库.如果你刚开始学习P ...
- pandas.DataFrame
1.可以使用单个列表或列表列表创建数据帧(DataFrame). 单个列表 import pandas as pd data = [1,2,3,4,5] df = pd.DataFrame(data) ...
- Node.js数据流Stream之Duplex流和Transform流
Duplex流一个很好的例子是TCP套接字连接.需要实现_read(size)和_Write(data,encoding,callback)方法. var stream = require('stre ...
- Unity3D性能优化小tips——把this.transform缓存缓存起来
Unity3D开发时中有一个小tips,这在官方的文档里其实有提及的,但不那么显眼,这里小说一下: 在MonoBehaviour进行编程时,我们经常会用this.transform, this.gam ...
随机推荐
- Codeforces 1221B. Knights
传送门 看到棋盘上跳马,发现如果把棋盘黑白染色,那么每次移动都是从白点到黑点,从黑点到白点 所以直接根据黑白染色判断每个位置的马的颜色即可 #include<iostream> #incl ...
- windows下生成zlib1.dll
一.原料: VC zlib-1.2.3-src.zip 二.解压zlib-1.2.3-src.zip,用VC打开工作空间 src/zlib/1.2.3/zlib-1.2.3/projects/visu ...
- C# using用法之一(命名空间相关)
通过using关键字可以引入命名空间 using System; using System.Collections.Generic; using System.Linq; using System.T ...
- openlayers之全屏控件的使用
import { FullScreen } from 'ol/control' map.addControl(new FullScreen())
- 多线程编程-- part5.1 互斥锁ReentrantLock
ReentrantLock简介 Reentrantlock是一个可重入的互斥锁,又被称为独占锁. Reentrantlock:分为公平锁和非公平锁,它们的区别体现在获取锁的机制上是否公平.“锁”是为了 ...
- 全自动链接克隆KVM虚拟机
virt-clone这个命令是基于全克隆的,也就是拷贝虚拟磁盘文件和虚拟配置文件来实现的完整克隆,速度慢,占用空间多 kvm软件包中并没有实现全自动链接克隆的命令或工具,只能手动实现,于是我决定写一个 ...
- 20199319《Linux内核原理与分析》第十一周作业
ShellShock攻击实验 什么是ShellShock Shellshock,又称Bashdoor,是在Unix中广泛使用的Bash shell中的一个安全漏洞,首次于2014年9月24日公开.许多 ...
- Java并发(基础知识)—— Executor框架及线程池
在Java并发(基础知识)—— 创建.运行以及停止一个线程中讲解了两种创建线程的方式:直接继承Thread类以及实现Runnable接口并赋给Thread,这两种创建线程的方式在线程比较少的时候是没有 ...
- 11jsp
1.JSP 1. 指令 作用:用于配置JSP页面,导入资源文件 格式: <%@ 指令名称 属性名1=属性值1 属性名2=属性值2 ... %> 分类: ...
- 第五章 动画 44:动画-使用第三方animate.css类库实现动画
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...