如何在Python中的SArray中将每个元素中的每个值相乘?

时间:2016-05-31 08:41:46

标签: python pandas graphlab

我使用的是Graphlab,但我想这个问题可以适用于pandas。

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'

3 个答案:

答案 0 :(得分:1)

我正在使用pandas数据框,但它也适用于您的情况。

import pandas as pd
df['new']=[dict((k,v*y) for k,v in x.items()) for x, y in zip(df['user_score'], df['weight'])]

输入数据框:

df
Out[34]: 
   id          user_score  weight
0   1  {u'a': 4, u'b': 3}       4
1   2  {u'a': 5, u'b': 7}       5
2   3  {u'a': 2, u'b': 3}       2

<强>输出:

df
Out[36]: 
   id          user_score  weight                   new
0   1  {u'a': 4, u'b': 3}       4  {u'a': 16, u'b': 12}
1   2  {u'a': 5, u'b': 7}       5  {u'a': 25, u'b': 35}
2   3  {u'a': 2, u'b': 3}       2    {u'a': 4, u'b': 6}

答案 1 :(得分:0)

这是许多可能的解决方案之一:

In [69]: df
Out[69]:
   id        user_score  weight
0   1  {'b': 3, 'a': 4}       4
1   2  {'b': 7, 'a': 5}       5
2   3  {'b': 3, 'a': 2}       2

In [70]: df['user_score'] = df['user_score'].apply(lambda x: pd.Series(x)).mul(df.weight, axis=0).to_dict('record')

In [71]: df
Out[71]:
   id          user_score  weight
0   1  {'b': 12, 'a': 16}       4
1   2  {'b': 35, 'a': 25}       5
2   3    {'b': 6, 'a': 4}       2

答案 2 :(得分:0)

这很微妙,但我认为你想要的是:

global sort
sort:
        push    ebp
    mov ebp, esp
    push    edi
    push    esi
    push    ebx
    sub esp, 28
    mov eax, dword [ebp+8]
    mov esi, dword [ebp+12]
    mov dword [ebp-32], 1
    lea edi, [eax-1]
    sal eax, 2
    lea edx, [eax+18]
    movss   xmm1, dword [esi-4+eax]
    mov dword [ebp-28], edi
    and edx, -16
    sub esp, edx
    lea edx, [esp+3]
    shr edx, 2
    lea ebx, [0+edx*4]
    mov dword [4+edx*4], edi
    mov dword [0+edx*4], 0
    xor edi, edi
    mov dword [ebp-36], ebx
.L14:
    mov eax, dword [ebp-28]
    lea edx, [edi-1]
    cmp eax, edi
    lea ebx, [esi+eax*4]
    jle .L15
    lea eax, [esi+edi*4]
.L18:
    movss   xmm0, dword [eax]
    comiss  xmm1, xmm0
    jb  .L16
    add edx, 1
    lea ecx, [esi+edx*4]
    movss   xmm2, dword [ecx]
    movss   dword [ecx], xmm0
    movss   dword [eax], xmm2
.L16:
    add eax, 4
    cmp eax, ebx
    jne .L18
    movss   xmm1, dword [ebx]
.L15:
    lea eax, [esi+4+edx*4]
    cmp edx, edi
    movss   xmm0, dword [eax]
    movss   dword [eax], xmm1
    movss   dword [ebx], xmm0
    jg  .L19
    sub dword [ebp-32], 2
    lea edi, [edx+2]
    cmp edi, dword [ebp-28]
    jl  .L25
.L21:
    mov eax, dword [ebp-32]
    test    eax, eax
    js  .L11
    mov edi, dword [ebp-36]
    lea eax, [edi+eax*4]
    mov edi, dword [eax]
    mov ecx, edi
    mov dword [ebp-28], edi
    mov edi, dword [eax-4]
    movss   xmm1, dword [esi+ecx*4]
    jmp .L14
.L19:
    mov edi, dword [ebp-32]
    mov eax, dword [ebp-36]
    mov dword [eax+edi*4], edx
    lea edi, [edx+2]
    cmp edi, dword [ebp-28]
    jge .L21
.L25:
    mov ebx, dword [ebp-32]
    mov ecx, dword [ebp-36]
    movaps  xmm1, xmm0
    lea eax, [ecx+ebx*4]
    mov ecx, dword [ebp-28]
    mov dword [eax+4], edi
    mov dword [eax+8], ecx
    mov eax, ebx
    add eax, 2
    mov dword [ebp-32], eax
    jns .L14
.L11:
    lea esp, [ebp-12]
    pop ebx
    pop esi
    pop edi
    pop ebp
    ret

apply函数将函数作为其参数,并将每行作为参数传递给该函数。在您的版本中,您在调用apply之前正在评估trans函数,这就是为什么错误消息会抱怨在期望dict时将SArray传递给trans函数。