我正在使用 Graphlab,但我想这个问题可以适用于熊猫。
import graphlab
sf = graphlab.SFrame({'id': [1, 2, 3], 'user_score': [{"a":4, "b":3}, {"a":5, "b":7}, {"a":2, "b":3}], 'weight': [4, 5, 2]})
我想创建一个新列,其中“user_score”中每个元素的值乘以“weight”中的数字。那是,
sf = graphlab.SFrame({'id': [1, 2, 3], 'user_score': [{"a":4, "b":3}, {"a":5, "b":7}, {"a":2, "b":3}], 'weight': [4, 5, 2]}, 'new':[{"a":16, "b":12}, {"a":25, "b":35}, {"a":4, "b":6}])
我尝试在下面编写一个简单的函数并应用无济于事。有什么想法吗?
def trans(x, y):
d = dict()
for k, v in x.items():
d[k] = v*y
return d
sf.apply(trans(sf['user_score'], sf['weight']))
它收到以下错误消息:
AttributeError: 'SArray' object has no attribute 'items'