Fork me on GitHub

python-for-data-高阶应用transform

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

参数详解

1
2
3
4
DataFrame.transform(self, func, axis=0, *args, **kwargs) → 'DataFrame'[source]
Call func on self producing a DataFrame with transformed values.

Produced DataFrame will have same axis length as self.
  • 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.

1
2
import numpy as np
import pandas as pd

transform方法

特点

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

  • 产生一个标量值,并且广播到各分组的尺寸数据中
  • transform可以产生一个和输入尺寸相同的对象
  • transform不可改变它的输入
1
2
3
4
5
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

分组

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

transform使用

每个位置被均值取代

传递匿名函数
1
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

1
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的函数一起使用
1
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

降序排名

1
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中直接传递函数

1
2
3
4
def normalize(x):
return (x - x.mean()) / x.std()

g.transform(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
1
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

归一化实例

1
2
normalized = (df["values"] - g.transform("mean")) / g.transform("std") # 内置的聚合函数直接传递
normalized
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

官方实例

1
2
df = pd.DataFrame({'A': range(3), 'B': range(1, 4)})
df
A B
0 0 1
1 1 2
2 2 3
1
df.transform(lambda x:x+1)  # 每个元素+1
A B
0 1 2
1 2 3
2 3 4
1
2
s = pd.Series(range(3))
s
0    0
1    1
2    2
dtype: int64
1
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方法的使用

原始数据

求解问题

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

1
2
3
4
5
6
7
8
9
10
import pandas as pd
df = pd.read_excel("sales_transactions.xlsx")

df.groupby('order')["ext price"].sum()

order
10001 576.12
10005 8185.49
10006 3724.49
Name: ext price, dtype: float64

1
2
3
order_total = df.groupby('order')["ext price"].sum().rename("Order_Total").reset_index()  # 添加Order_Total列属性的值
df_1 = df.merge(order_total) # 合并原始数据df和order_total数据
df_1["Percent_of_Order"] = df_1["ext price"] / df_1["Order_Total"] # 添加Percent_of_Order

使用transform

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

图解transform

本文标题:python-for-data-高阶应用transform

发布时间:2020年05月20日 - 23:05

原始链接:http://www.renpeter.cn/2020/05/20/python-for-data-%E9%AB%98%E9%98%B6%E5%BA%94%E7%94%A8transform.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea