捷徑

torcharrow.DataFrame.map

DataFrame.map(arg: ty.Union[ty.Dict, ty.Callable], na_action: ty.Literal['ignore', None] = None, dtype: ty.Optional[dt.DType] = None, columns: ty.Optional[ty.List[str]] = None)

根據輸入的對應關係映射資料列。

參數:
  • callable (arg - dict 或) – 如果 arg 是一個 dict,則使用此 dict 映射輸入,未映射的值將變為 null。如果 arg 是一個可呼叫物件,則將其視為使用者定義函數 (UDF),並在輸入的每個元素上呼叫。可呼叫物件必須是全域函數或類別實例上的方法,不支援 lambda 函數。

  • None (預設) – 如果您的 UDF 對 null 輸入返回 null,選擇「忽略」可以提高效率,因為 map 將避免對 null 值呼叫您的 UDF。如果為 None,則一律呼叫 UDF。

  • None – 如果您的 UDF 對 null 輸入返回 null,選擇「忽略」可以提高效率,因為 map 將避免對 null 值呼叫您的 UDF。如果為 None,則一律呼叫 UDF。

  • DType (dtype -) – DType 用於強制輸出類型。如果結果類型 != 項目類型,則需要 DType。

  • None – DType 用於強制輸出類型。如果結果類型 != 項目類型,則需要 DType。

  • names (columns - 資料行列表) – 決定要提供給映射 dict 或 UDF 的資料行。

  • None – 決定要提供給映射 dict 或 UDF 的資料行。

另請參閱

flatmap, filter

範例

>>> import torcharrow as ta
>>> ta.column([1,2,None,4]).map({1:111})
0  111
1  None
2  None
3  None
dtype: Int64(nullable=True), length: 4, null_count: 3

使用 defaultdict 提供遺漏值

>>> from collections import defaultdict
>>> ta.column([1,2,None,4]).map(defaultdict(lambda: -1, {1:111}))
0  111
1   -1
2   -1
3   -1
dtype: Int64(nullable=True), length: 4, null_count: 0

使用使用者提供的 Python 函數

>>> def add_ten(num):
>>>     return num + 10
>>>
>>> ta.column([1,2,None,4]).map(add_ten, na_action='ignore')
0  11
1  12
2  None
3  14
dtype: Int64(nullable=True), length: 4, null_count: 1

請注意,上例中的 .map(add_ten, na_action=None) 會因為類型錯誤而失敗,因為 addten 並未針對 None/null 定義。要將 null 傳遞給 UDF,UDF 需要做好準備

>>> def add_ten_or_0(num):
>>>     return 0 if num is None else num + 10
>>>
>>> ta.column([1,2,None,4]).map(add_ten_or_0, na_action=None)
0  11
1  12
2   0
3  14
dtype: Int64(nullable=True), length: 4, null_count: 0

映射到不同類型需要 dtype 參數

>>> ta.column([1,2,None,4]).map(str, dtype=dt.string)
0  '1'
1  '2'
2  'None'
3  '4'
dtype: string, length: 4, null_count: 0

映射 DataFrame 時,UDF 會將整列作為一個 tuple 獲取

>>> def add_unary(tup):
>>>     return tup[0]+tup[1]
>>>
>>> ta.dataframe({'a': [1,2,3], 'b': [1,2,3]}).map(add_unary , dtype = dt.int64)
0  2
1  4
2  6
dtype: int64, length: 3, null_count: 0

多參數 UDF

>>> def add_binary(a,b):
>>>     return a + b
>>>
>>> ta.dataframe({'a': [1,2,3], 'b': ['a', 'b', 'c'], 'c':[1,2,3]}).map(add_binary, columns = ['a','c'], dtype = dt.int64)
0  2
1  4
2  6
dtype: int64, length: 3, null_count: 0

多返回值 UDF - 可以透過返回 DataFrame(也稱為 struct 資料行)來指定返回多個資料行的函數;必須提供返回 dtype

>>> ta.dataframe({'a': [17, 29, 30], 'b': [3,5,11]}).map(divmod, columns= ['a','b'], dtype = dt.Struct([dt.Field('quotient', dt.int64), dt.Field('remainder', dt.int64)]))
  index    quotient    remainder
-------  ----------  -----------
      0           5            2
      1           5            4
      2           2            8
dtype: Struct([Field('quotient', int64), Field('remainder', int64)]), count: 3, null_count: 0

可以透過在(資料)類別中擷取狀態並使用方法作為委託來編寫具有狀態的 UDF

>>> def fib(n):
>>>     if n == 0:
>>>         return 0
>>>     elif n == 1 or n == 2:
>>>         return 1
>>>     else:
>>>         return fib(n-1) + fib(n-2)
>>>
>>> from dataclasses import dataclass
>>> @dataclass
>>> class State:
>>>     state: int
>>>     def __post_init__(self):
>>>         self.state = fib(self.state)
>>>     def add_fib(self, x):
>>>         return self.state+x
>>>
>>> m = State(10)
>>> ta.column([1,2,3]).map(m.add_fib)
0  56
1  57
2  58
dtype: int64, length: 3, null_count: 0

文件

存取 PyTorch 的完整開發人員文件

檢視文件

教學

取得適用於初學者和進階開發人員的深入教學課程

檢視教學課程

資源

尋找開發資源並獲得問題解答

檢視資源