Pandas DataFrame.transform(~) 方法应用一个函数来转换源 DataFrame 的行或列。请注意,返回了新的DataFrame,并且源DataFrame保持不变。
参数
1.func | function 或 string 或 list 或 dict
应用于源 DataFrame 的行或列的转换。如果传递一个函数,那么它会接受一个 Series 或 DataFrame 作为参数。
允许的值如下:
- 
函数(例如 np.mean)
- 
字符串形式的函数名称(例如 "np.mean")
- 
上述两者的列表(例如 [np.mean, "np.max"])
- 
字典: - 
key:行/列标签 
- 
value:函数、函数名称或此类列表 
 
- 
2. axis | list | optional
是否按行或按列应用转换:
| 轴 | 说明 | 
|---|---|
| 
 | 变换每一列。 | 
| 
 | 变换每一行。 | 
默认情况下,axis=0 。
3. args | any
您想要传递给 func 的位置参数。
4. kwargs | any
要传递给 func 的关键字参数。
返回值
一个新的DataFrame,其形状与源 DataFrame 相同。
例子
基本用法
考虑以下 DataFrame :
df = pd.DataFrame({"A":[-3,4],"B":[5,-6]})
df
   A   B
0  -3  5
1  4   -6应用 NumPy 的  abs(~)  方法,该方法返回输入的绝对值:
df.transform(np.abs)   # or "np.abs"
   A  B
0  3  5
1  4  6传入一个函数
考虑以下 DataFrame :
df = pd.DataFrame({"A":[3,4],"B":[5,6]})
df
   A  B
0  3  5
1  4  6我们可以传入一个自定义函数,如下所示:
def foo(col):
 return 2 * col if np.sum(col) >= 10 else col
df.transform(foo)
   A  B
0  3  10
1  4  12在这里,我们的自定义函数 foo 接受参数 col ,它是 DataFrame ( Series ) 的单列。
传入多个函数
考虑以下 DataFrame :
df = pd.DataFrame({"A":[-3,4],"B":[-5,6]})
df
   A   B
0  -3  -5
1  4   6要应用多个转换,请传入一个列表,如下所示:
df.transform([np.abs, lambda x: x + 1])
   A                     B
   absolute  <lambda_0>  absolute  <lambda_0>
0     3         -2          5         -4
1     4         5           6         7注意这两个变换是如何进行的独立的,也就是说,这两个变换都应用于原始值。
变换每一列
考虑以下 DataFrame :
df = pd.DataFrame({"A":[3,4],"B":[5,6]})
df
   A  B
0  3  5
1  4  6默认情况下, axis=0 ,这意味着我们正在转换每一列:
def foo(col):
 return 2 * col if np.sum(col) >= 10 else col
df.transform(foo)   # axis=0
   A  B
0  3  10
1  4  12变换每一行
考虑与之前相同的DataFrame:
df = pd.DataFrame({"A":[3,4],"B":[5,6]})
df
   A  B
0  3  5
1  4  6要转换每一行,请传入 axis=1,如下所示:
def foo(row):   # row is a Series representing a single row
 return 2 * row if np.sum(row) >= 10 else row
df.transform(foo, axis=1)
   A  B
0  3  5
1  8  12仅转换某些列
考虑与之前相同的DataFrame:
df = pd.DataFrame({"A":[3,4],"B":[5,6],"C":[7,8]})
df
   A  B  C
0  3  5  7
1  4  6  8默认情况下,transform(~) 方法将转换所有列 ( axis=0 ) 或所有行 ( axis=1 )。
要转换某些列,请先选择要转换的列:
def foo(val):
 return val * 3
# Here, we are transforming just columns A and B
df_new_cols = df[["A","B"]].transform(foo)
df_new_cols
   A   B
0  9   15
1  12  18此处,返回具有转换列的新DataFrame,而原始DataFrame df 保持不变。如果你想替换原来的列,那么:
def foo(val):
 return val * 3
df_new_cols = df[["A","B"]].transform(foo)
df[["A","B"]] = df_new_cols
df
   A   B   C
0  9   15  7
1  12  18  8传递参数
考虑以下 DataFrame :
df = pd.DataFrame({"A":[3,4],"B":[5,6]})
df
   A  B
0  3  5
1  4  6将参数传递给 func :
def foo(x, threshold):
 return 2 * x if np.sum(x) >= threshold else x
df.transform(foo, threshold=10)
   A  B
0  3  10
1  4  12相关用法
- Python PySpark DataFrame transform方法用法及代码示例
- Python Pandas DataFrame transpose方法用法及代码示例
- Python Pandas DataFrame truncate方法用法及代码示例
- Python Pandas DataFrame truediv方法用法及代码示例
- Python Pandas DataFrame tz_convert方法用法及代码示例
- Python Pandas DataFrame tail方法用法及代码示例
- Python Pandas DataFrame to_csv方法用法及代码示例
- Python Pandas DataFrame tz_localize方法用法及代码示例
- Python PySpark DataFrame toDF方法用法及代码示例
- Python PySpark DataFrame toJSON方法用法及代码示例
- Python Pandas DataFrame tshift方法用法及代码示例
- Python Pandas DataFrame to_period方法用法及代码示例
- Python Pandas DataFrame take方法用法及代码示例
- Python Pandas DataFrame to_json方法用法及代码示例
- Python PySpark DataFrame tail方法用法及代码示例
- Python PySpark DataFrame toPandas方法用法及代码示例
- Python Pandas DataFrame to_timestamp方法用法及代码示例
- Python Pandas DataFrame to_numpy方法用法及代码示例
- Python Pandas DataFrame to_dict方法用法及代码示例
- Python PySpark DataFrame take方法用法及代码示例
- Python Pandas DataFrame empty属性用法及代码示例
- Python Pandas DataFrame pop方法用法及代码示例
- Python Pandas DataFrame nsmallest方法用法及代码示例
- Python Pandas DataFrame sample方法用法及代码示例
- Python Pandas DataFrame items方法用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 Pandas DataFrame | transform method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
