mars.dataframe.DataFrame.map_chunk#

DataFrame.map_chunk(func, args=(), kwargs=None, skip_infer=False, **kw)#

将函数应用于每个块。

Parameters
  • func (function) – 应用于每个块的函数。

  • args (tuple) – 除了数组/序列外,还要传递给func的定位参数。

  • kwargs (Dict) – 额外的关键字参数作为关键字参数传递给func。

  • skip_infer (bool, 默认值为 False) – 当未指定 dtypes 或 output_type 时,是否推断数据类型。

Returns

对DataFrame或Series每个块应用func的结果。

Return type

序列数据框

另请参阅

DataFrame.apply

执行任何类型的操作。

示例

>>> import mars.dataframe as md
>>> df = md.DataFrame([[4, 9]] * 3, columns=['A', 'B'])
>>> df.execute()
   A  B
0  4  9
1  4  9
2  4  9

输出类型包括 Series 或 DataFrame 将自动推断。

>>> df.map_chunk(lambda c: c['A'] + c['B']).execute()
0    13
1    13
2    13
dtype: int64

如果自动推断失败,您可以自行指定 output_type

>>> import pandas as pd
>>> import numpy as np
>>> df['c'] = ['s1', 's2', 's3']
>>> df.map_chunk(lambda c: pd.concat([c['A'], c['c'].str.slice(1).astype(int)], axis=1)).execute()
Traceback (most recent call last):
TypeError: Cannot determine `output_type`, you have to specify it as `dataframe` or `series`...
>>> df.map_chunk(lambda c: pd.concat([c['A'], c['c'].str.slice(1).astype(int)], axis=1),
>>>              output_type='dataframe', dtypes=pd.Series([np.dtype(object), np.dtype(int)])).execute()
   A  c
0  4  1
1  4  2
2  4  3