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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
