1、官网解析
参考地址:
SFrame.
apply
(fn, dtype=None, seed=None)
Transform each row to an SArray
according to a specified function. Returns a new SArray of dtype
where each element in this SArray is transformed by fn(x) where x is a single row in the sframe represented as a dictionary. The fn
should return exactly one value which can be cast into type dtype
. If dtype
is not specified, the first 100 rows of the SFrame are used to make a guess of the target data type.
Parameters: | fn : function
dtype : dtype, optional
seed : int, optional
|
---|---|
Returns: | out : SArray
|
Examples
Concatenate strings from several columns:
>>> sf = graphlab.SFrame({'user_id': [1, 2, 3], 'movie_id': [3, 3, 6],
'rating': [4, 5, 1]})
>>> sf.apply(lambda x: str(x['user_id']) + str(x['movie_id']) + str(x['rating']))
dtype: str
Rows: 3
['134', '235', '361']
2、示例
def great_count(word_count_vector):
if 'great' in word_count_vector:
return word_count_vector['great']
else:
return 0
products['great'] = products['word_count'].apply(great_count, int)
products['great'].show()
products['word_count']是个字典数组,对products['word_count']的每一个元素都进行apply中的函数great_count。
加上Int类型,是为了不仅仅计算前100个数,而是整个数组。这一段见我的博文:
https://blog.csdn.net/weixin_41770169/article/details/80808393