从列表数组聚合键值的更快方法,而不是使用pandas groupby

时间:2019-07-11 14:31:11

标签: python pandas list pandas-groupby aggregation

我正在处理一个功能正常的函数,但它的速度不如我希望的那样快。此函数具有一类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键来获取字典数组,其中键var0055var0054的值由键var0046具有相同值的对象,例如:

data = [{
            "var0046":"tr100",
            "var0055":165,
            "var0054":15000
         },
         {
            "var0046":"tr200",
            "var0055":12,
            "var0054":8000
         },
         {
            "var0046":"tr300",
            "var0055":3,
            "var0054":30000
         }]  
 }]

我的最终目标是得到var0055var0054的值的数组,其中数组的每个位置代表键的对象的值除以所有值的总和同一键中的对象,例如:

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

为什么要汇总此命令以更快地获得ind0054ind0055的结果?无需转换为熊猫数据框?直接使用dict格式吗?

0 个答案:

没有答案