本文中详解介绍了pandas中transform()方法的使用
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.
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.
{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.
Positional arguments to pass to func.
Keyword arguments to pass to func.
A DataFrame that must have the same length as self.
If the returned DataFrame has a different length than self.
import numpy as np
import pandas as pd
transform方法通常是和groupby方法一起连用的
df = pd.DataFrame({
"key":["a","b","c"] * 4,
"values":np.arange(12.0)
})
df
.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </code></pre>
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 # 分组再求平均
g.mean()
key
a 4.5
b 5.5
c 6.5
Name: values, dtype: float64
每个位置被均值取代
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
内建的聚合函数直接传递别名,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
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
向tranform中直接传递函数
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
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") # 内置的聚合函数直接传递
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
df = pd.DataFrame({'A': range(3), 'B': range(1, 4)})
df
.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </code></pre>
A | B | |
---|---|---|
0 | 0 | 1 |
1 | 1 | 2 |
2 | 2 | 3 |
df.transform(lambda x:x+1) # 每个元素+1
.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </code></pre>
A | B | |
---|---|---|
0 | 1 | 2 |
1 | 2 | 3 |
2 | 3 | 4 |
s = pd.Series(range(3))
s
0 0
1 1
2 2
dtype: int64
s.transform([np.sqrt, np.exp]) # 传入函数即可
.dataframe tbody tr th:only-of-type { vertical-align: middle; } <pre><code>.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </code></pre>
sqrt | exp | |
---|---|---|
0 | 0.000000 | 1.000000 |
1 | 1.000000 | 2.718282 |
2 | 1.414214 | 7.389056 |
在这个网站上有一个完整的实例,解释了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:
求出不同商品在所在订单的价钱占比
先求出一列占比的值,再和原始数据进行合并merge
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
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 + groupby连用:先分组再求和