• 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']
    
    

    Groupby Example

    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
    

    Groupby Example

    what is happening in transform
  • 相关阅读:
    有关程序开发中有关验证中常用的正则表达式汇总
    python学习---logging模块
    有关递归函数,返回值的学习
    设计模式之建造者模式、模版方法
    XXL-JOB使用命令行的方式启动python时,日志过多导致阻塞的解决方式
    Spring Boot后端与Angular前端进行timestamp的交互
    设计模式之代理模式
    设计模式之工厂模式
    设计模式之单例模式
    设计模式之反射机制
  • 原文地址:https://www.cnblogs.com/ZeroTensor/p/10244074.html
Copyright © 2020-2023  润新知