Pandas DataFrame.apply(~) 將指定的函數應用於 DataFrame 的每一行或每一列。
參數
1. func | function
沿行或列應用的函數。
2. axis | string 或 int | optional
執行該函數所沿的軸:
|
軸 |
說明 |
|---|---|
|
|
函數將應用於每一列。 |
|
|
函數將應用於每一行。 |
默認情況下,axis=0 。
3. raw | boolean | optional
-
如果
True,則 NumPy 數組將作為func的參數傳遞。 -
如果
False,則將傳遞一個係列。
Performance-wise,如果您要應用還原 Numpy 函數,例如 np.sum ,則選擇 raw=True 。默認情況下,raw=False 。
4. result_type | string 或 None | optional
如何解析 func 的 list-like 返回值。這僅在 axis=1 時相關(當 func 逐行應用時):
|
值 |
說明 |
|---|---|
|
|
list-like 結果的值(例如 |
|
|
list-like 結果的值將減少為單個係列。 |
|
|
list-like 結果的值將被分成列,但與 |
|
|
行為取決於函數返回的值。如果返回 |
默認情況下,result_type=None 。請參閱下麵的示例以進行說明。
5. args | tuple | optional
您想要提供給 func 的其他位置參數。
6. **kwds | optional
您想要提供給 func 的其他關鍵字參數。
返回值
應用函數後生成的Series 或DataFrame。
例子
在列上應用函數
考慮以下 DataFrame :
df = pd.DataFrame({"A":[2,3], "B":[4,5]})
df
A B
0 2 4
1 3 5
要按列應用 np.sum 函數:
df.apply(np.sum) # axis=0
A 5
B 9
dtype: int64
注意
如果您在應用 NumPy 還原函數(如 np.sum )時設置 raw=True ,Pandas 可以從性能提升中受益。
在行上應用函數
考慮與之前相同的DataFrame:
df = pd.DataFrame({"A":[2,3], "B":[4,5]})
df
A B
0 2 4
1 3 5
要按行應用 np.sum 函數,請設置 axis=1 :
df.apply(np.sum, axis=1) # Returns a Series
0 6
1 8
dtype: int64
應用內置捆綁器
考慮與之前相同的DataFrame:
df = pd.DataFrame({"A":[2,3], "B":[4,5]})
df
A B
0 2 4
1 3 5
您可以使用內置函數(例如 tuple 、 list 甚至 Series )捆綁值:
df.apply(tuple)
A (2, 3)
B (4, 5)
dtype: object
應用自定義函數
考慮與之前相同的DataFrame:
df = pd.DataFrame({"A":[2,3], "B":[4,5]})
df
A B
0 2 4
1 3 5
應用自定義函數:
def foo(col):
return 2 * col
df.apply(foo)
A B
0 4 8
1 6 10
我們的函數 foo 接受 Series 類型的列 ( axis=0 ) 作為參數,並將轉換後的列作為 Series 返回。
傳入關鍵字參數
要將關鍵字參數傳遞給 func :
def foo(col, x):
return x * col
df.apply(foo, x=2)
A B
0 4 8
1 6 10
解析list-like返回值的不同方式
考慮以下 DataFrame :
df = pd.DataFrame({"A":[4,5], "B":[6,7]}, index=["a","b"])
df
A B
a 4 6
b 5 7
當函數的返回類型為 list-like 時,參數 result_type 起作用。
返回類型 無
當 return_type 未設置時,默認行為是將 list-like 返回值放在 Series 中:
df.apply(lambda x: [1,2,3], axis=1) # Returns a Series
a [1, 2, 3]
b [1, 2, 3]
dtype: object
請注意,lambda x: [1,2,3] 相當於以下內容:
def foo(x): # The function name isn't important here
return [1,2,3]
返回類型 展開
當 return_type="expand" 時,list-like 的值將被分成列,從而產生 DataFrame :
df.apply(lambda x: [1,2,3], axis=1, result_type="expand") # Returns a DataFrame
0 1 2
a 1 2 3
b 1 2 3
請注意,我們不再擁有原始列名稱 A 和 B 。
返回類型廣播
當 return_type="broadcast" 時,list-like 值將被分成列,但與 "expand" 不同,列名稱將被保留:
df.apply(lambda x: [1,2], axis=1, result_type="broadcast") # Returns a DataFrame
A B
a 1 2
b 1 2
為此,list-like 的長度必須等於源 DataFrame 中的列數。這意味著在這種情況下返回 [1,2,3] 而不是 [1,2] 將導致錯誤。
相關用法
- Python Pandas DataFrame applymap方法用法及代碼示例
- Python Pandas DataFrame append方法用法及代碼示例
- Python Pandas DataFrame agg方法用法及代碼示例
- Python Pandas DataFrame all方法用法及代碼示例
- Python Pandas DataFrame add方法用法及代碼示例
- Python Pandas DataFrame asfreq方法用法及代碼示例
- Python Pandas DataFrame any方法用法及代碼示例
- Python PySpark DataFrame alias方法用法及代碼示例
- Python Pandas DataFrame asof方法用法及代碼示例
- Python Pandas DataFrame add_prefix方法用法及代碼示例
- Python Pandas DataFrame add_suffix方法用法及代碼示例
- Python Pandas DataFrame at屬性用法及代碼示例
- Python Pandas DataFrame axes屬性用法及代碼示例
- Python Pandas DataFrame astype方法用法及代碼示例
- Python Pandas DataFrame align方法用法及代碼示例
- Python Pandas DataFrame assign方法用法及代碼示例
- Python Pandas DataFrame at_time方法用法及代碼示例
- Python Pandas DataFrame abs方法用法及代碼示例
- Python Pandas DataFrame empty屬性用法及代碼示例
- Python Pandas DataFrame pop方法用法及代碼示例
- Python Pandas DataFrame nsmallest方法用法及代碼示例
- Python Pandas DataFrame sample方法用法及代碼示例
- Python Pandas DataFrame items方法用法及代碼示例
- Python Pandas DataFrame max方法用法及代碼示例
- Python Pandas DataFrame swaplevel方法用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 Pandas DataFrame | apply method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
