zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    正则表达式
    爬虫原理和网页构造
    简单的博客系统之二
    配置编辑器geany
    linux删除多文件
    eNSP交换路由基础
    NTP centOS6.5
    shell脚本之lftp上传
    进度条
    maketrans与translate函数
  • 原文地址:https://www.cnblogs.com/ZeroTensor/p/10244074.html
Copyright © 2011-2022 走看看