我正在处理一个功能正常的函数,但它的速度不如我希望的那样快。此函数具有一类dict数组作为该类型的输入:
item = [{
"var0057":31,
"var0001":"A",
"var0002":2,
"data":[{
"var0046":"tr100",
"var0055":55,
"var0054":1000,
"var0058":2038
},
{
"var0046":"tr200",
"var0055":12,
"var0054":8000,
"var0058":2038
}]
},
{
"var0057":31,
"var0001":"B",
"var0002":3,
"data":[{
"var0046":"tr100",
"var0055":110,
"var0054":14000,
"var0058":2038
},
{
"var0046":"tr300",
"var0055":3,
"var0054":30000,
"var0058":2038
}]
}]
因此,我使用此字典数组的中目标是仅使用data
键来获取字典数组,其中键var0055
和var0054
的值由键var0046
具有相同值的对象,例如:
data = [{
"var0046":"tr100",
"var0055":165,
"var0054":15000
},
{
"var0046":"tr200",
"var0055":12,
"var0054":8000
},
{
"var0046":"tr300",
"var0055":3,
"var0054":30000
}]
}]
我的最终目标是得到var0055
和var0054
的值的数组,其中数组的每个位置代表键的对象的值除以所有值的总和同一键中的对象,例如:
sum_var0054 = 15000+8000+30000
var0054 = [15000/sum_var0054,8000/sum_var0054,30000/sum_var0054]
我的代码正在运行,但是很慢:
def my_func(response):
data2 = []
for items in response['item']:
data2.extend(items['data'])
response2 = pd.DataFrame(data2)
response2 = response2.drop(columns = ['var0058'])
response2 = response2.groupby('var0046', as_index=False).sum()
sum_var0054 = sum(response2['var0054'])
ind0054 = sum((response2['var0054'] ** 2)/sum_var0054)
sum_var0055 = sum(response2['var0055'])
ind0055 = sum((response2['var0055'] ** 2)/sum_var0055)
response.clear()
response['ind0054'] = ind0054
response['ind0055'] = ind0055
为什么要汇总此命令以更快地获得ind0054
和ind0055
的结果?无需转换为熊猫数据框?直接使用dict格式吗?