2015-05-05 34 views
0

我寫的工作原理如下功能:matrix[24787][24788] = generate_value(source, 24787, 24788)如何將一個數字設計的函數應用於熊貓的整個數據框?

我想概括這matrix = generate_value(source, <array of indices>, <array of columns>)和已經填充了整個矩陣我怎麼能做到這一點,寫短兩個循環的?

編輯:這是generate_value -

def generate_value(source, id_a, id_b): 
    intersection = pd.merge(source.get_group(id_a), source.get_group(id_b), how='outer', on='merge_field') 
    intersection.val_x.fillna(value=0, inplace=True) 
    intersection.val_y.fillna(value=0, inplace=True) 
    intersection.total_value_differences = abs(intersection.val_x - intersection.val_y) 
    return sum(intersection.total_value_differences) 

如果必要的話,我可以把它改寫。

+0

這很大程度上取決於如何寫入'generate_value'。在最簡單的情況下,由於NumPy處理諸如'array + other_array'之類的操作,它將會正常工作。在更糟糕的情況下,你可能會堅持把'numpy.vectorize'放在你的函數上,並獲得與兩個Python循環基本相同的性能。 – user2357112

+0

它並沒有自己的工作,'numpy.vectorize'矢量化'source'數據框,即使我明確地排除它如下:'newfunc = numpy.vectorize(generate_value,excluded = ['source'])''。我已經在問題中發佈了'generate_value'的源代碼。 –

回答

0

目前在熊貓看來這個功能是不可能的。

相關問題