利用Python进行数据分析(12)-高阶应用transform

本文中详解介绍了pandastransform()方法的使用

image

参数解释

DataFrame.transform(self, func, axis=0, *args, **kwargs) → 'DataFrame'[source]
  • funcfunction, str, list or dict Function to use for transforming the data. If a function, must either work when passed a DataFrame or when passed to DataFrame.apply.

Accepted combinations are:

- function

- string function name

- list of functions and/or function names, e.g. [np.exp. 'sqrt']

- dict of axis labels -> functions, function names or list of such.

  • axis

{0 or ‘index’, 1 or ‘columns’}, default 0 If 0 or ‘index’: apply function to each column. If 1 or ‘columns’: apply function to each row.

  • *args

Positional arguments to pass to func.

  • **kwargs

Keyword arguments to pass to func.

  • Returns:DataFrame

A DataFrame that must have the same length as self.

  • Raises:ValueError

If the returned DataFrame has a different length than self.

import numpy as np 

transform方法

特点

transform方法通常是和groupby方法一起连用的

  • 产生一个标量值,并且广播到各分组的尺寸数据中
  • transform可以产生一个和输入尺寸相同的对象
  • transform不可改变它的输入
df = pd.DataFrame({"key":["a","b","c"] * 4,"values":np.arange(12.0)})
df
key values
0 a 0.0
1 b 1.0
2 c 2.0
3 a 3.0
4 b 4.0
5 c 5.0
6 a 6.0
7 b 7.0
8 c 8.0
9 a 9.0
10 b 10.0
11 c 11.0

分组

g = df.groupby("key").values  # 分组再求平均
key
a    4.5
b    5.5
c    6.5
Name: values, dtype: float64

transform使用

每个位置被均值取代

传递匿名函数
g.transform(lambda x:x.mean())
0     4.5
1     5.5
2     6.5
3     4.5
4     5.5
5     6.5
6     4.5
7     5.5
8     6.5
9     4.5
10    5.5
11    6.5
Name: values, dtype: float64

传递agg方法中的函数字符串别名

内建的聚合函数直接传递别名,max\min\sum\mean

g.transform("mean")
0     4.5
1     5.5
2     6.5
3     4.5
4     5.5
5     6.5
6     4.5
7     5.5
8     6.5
9     4.5
10    5.5
11    6.5
Name: values, dtype: float64

tranform 和 返回S的函数一起使用
g.transform(lambda x:x * 2)
0      0.0
1      2.0
2      4.0
3      6.0
4      8.0
5     10.0
6     12.0
7     14.0
8     16.0
9     18.0
10    20.0
11    22.0
Name: values, dtype: float64

降序排名

g.transform(lambda x:x.rank(ascending=False))
0     4.0
1     4.0
2     4.0
3     3.0
4     3.0
5     3.0
6     2.0
7     2.0
8     2.0
9     1.0
10    1.0
11    1.0
Name: values, dtype: float64

类似apply功能

向tranform中直接传递函数

def normalize(x):
0    -1.161895
1    -1.161895
2    -1.161895
3    -0.387298
4    -0.387298
5    -0.387298
6     0.387298
7     0.387298
8     0.387298
9     1.161895
10    1.161895
11    1.161895
Name: values, dtype: float64

g.apply(normalize)  # 结果同上
0    -1.161895
1    -1.161895
2    -1.161895
3    -0.387298
4    -0.387298
5    -0.387298
6     0.387298
7     0.387298
8     0.387298
9     1.161895
10    1.161895
11    1.161895
Name: values, dtype: float64

归一化实例

normalized = (df["values"] - g.transform("mean")) / g.transform("std") # 内置的聚合函数直接传递
0    -1.161895
1    -1.161895
2    -1.161895
3    -0.387298
4    -0.387298
5    -0.387298
6     0.387298
7     0.387298
8     0.387298
9     1.161895
10    1.161895
11    1.161895
Name: values, dtype: float64

官方实例

df = pd.DataFrame({'A': range(3), 'B': range(1, 4)})
A B
0 0 1
1 1 2
2 2 3
df.transform(lambda x:x+1)  # 每个元素+1
A B
0 1 2
1 2 3
2 3 4
s = pd.Series(range(3))
0    0
1    1
2    2
dtype: int64

s.transform([np.sqrt, np.exp])  # 传入函数即可
sqrt exp
0 0.000000 1.000000
1 1.000000 2.718282
2 1.414214 7.389056

Understanding the Transform Function in Pandas

在这个网站上有一个完整的实例,解释了transform方法的使用

原始数据

image

求解问题

You can see in the data that the file contains 3 different orders (10001, 10005 and 10006) and that each order consists has multiple products (aka skus).

The question we would like to answer is: “What percentage of the order total does each sku represent?”

For example, if we look at order 10001 with a total of $576.12, the break down would be:

  • B1-20000 = $235.83 or 40.9%
  • S1-27722 = $232.32 or 40.3%
  • B1-86481 = $107.97 or 18.7%

求出不同商品在所在订单的价钱占比

传统方法

先求出一列占比的值,再和原始数据进行合并merge

import pandas as pd
image
order_total = df.groupby('order')["ext price"].sum().rename("Order_Total").reset_index()  # 添加Order_Total列属性的值
image

使用transform

Transform + groupby连用:先分组再求和

image
image

图解transform

image
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。