Forming a nested list and calculating the sum of fields

Viewed 60

The records are stored in the database in this form:

clu name a b
q one 7 6
q two 6 9
q five 6 10
e three 8 7
e four 10 4

I am writing a function that will calculate the sum of the given values for different clu variables (for example, q and e). That is, first it is determined which values of name correspond to the value of e (these are three and four). Then there is an appeal to the dictionary "e_dict", which stores certain values corresponding to different names. The function should determine that for group e it is necessary to add the values of the "name" keys three and four and give the result.

Example of a dictionary e_dict:

{'one': {'u_mean': 4.25, 'c_mean': 4.25}, 'three': {'u_mean': 4.5, 'c_mean': 4.5}, 'two': {'u_mean': 4.583333333333334, 'c_mean': 4.583333333333334}, 'four': {'u_mean': 4.5625, 'c_mean': 4.5625}, 'five': {'u_mean': 4.65, 'c_mean': 4.65}}

The result should be like this:

 {'e': {'u_mean': 4.531, 'c_mean': 4.531}, 'q': {'u_mean': 4.49443, 'c_mean': 4.49443}}

that is, the fields are all u_mean and their average is found, and they are also added.

The full code of my function:

def group_names():
    st, c_clus, n_names = [], [], []
    for h in Utilizations.objects.values('clu', 'name', 'a', 'b'):
        st.append((h.get('clu'), h.get('name'), h.get('a'), h.get('b')))
        c_clus.append(h.get('clu'))
        n_names.append(h.get('name'))
    """получение названий"""
    names, clus = [], []
    for nam in n_names:
        if nam not in names:
            names.append(nam)
    for cl in c_clus:
        if cl not in clus:
            clus.append(cl)
    clu, e = {}, {}
    u_load, u_max = {}, {}
    mean_all, u_load_mean, u_max_mean = 0, 0, 0
    for nam in names:
        hs = Utilizations.objects.filter(name=nam)
        o, p = 0, 0
        for h in hs:
            o += h.a
            p += h.b
            u_load[nam] = o / 2 + 1
            u_max[nam] = p / 2 + 1
        u_max_mean = mean(u_max.values())
        u_load_mean = mean(u_load.values())
        mean_all = (u_max_mean + u_load_mean) / 2
        e[nam] = {'u_mean': mean_all, 'c_mean': mean_all}
    for cl in clus:
        for nam in names:
            s = Utilizations.objects.filter(name=nam, clu=cl)
            for h in hs:
                clu[nam] = cl
    return clu

It turns out to group in this form: {'one': 'q', 'two': 'q', 'five': 'q', 'three': 'e', 'four': 'e'}

And I don't know what to do next(

2 Answers

I don't know how your original data is stored (I don't recognize "Utilizations.objects.values"), but here's code that will compute those averages based on a simple list of lists:


data = [
    ['q','one',7,6],
    ['q','two',7,6],
    ['q','five',7,6],
    ['e','three',7,6],
    ['e','four',7,6]
]

e_dict = {
        'one': {'u_mean': 4.25, 'c_mean': 4.25},
        'three': {'u_mean': 4.5, 'c_mean': 4.5},
        'two': {'u_mean': 4.583333333333334, 'c_mean': 4.583333333333334},
        'four': {'u_mean': 4.5625, 'c_mean': 4.5625},
        'five': {'u_mean': 4.65, 'c_mean': 4.65}
}

def group_names():
    sums = {}
    counts = {}
    for h in data:
        if h[0] not in sums:
            sums[h[0]] = { "u_mean": 0, "c_mean": 0 }
            counts[h[0]] = 0
        for k,v in e_dict[h[1]].items():
            sums[h[0]][k] += v
        counts[h[0]] += 1
    for k,v in sums.items():
        sums[k]['u_mean'] /= counts[k]
        sums[k]['c_mean'] /= counts[k]
    return sums

print(group_names())

Output:

{'q': {'u_mean': 4.4944444444444445, 'c_mean': 4.4944444444444445}, 'e': {'u_mean': 4.53125, 'c_mean': 4.53125}}

You can use pandas for this:

Input:

data = {'clu': {0: 'q', 1: 'q', 2: 'q', 3: 'e', 4: 'e'}, 'name': {0: 'one', 1: 'two', 2: 'five', 3: 'three', 4: 'four'}, 'a': {0: 7, 1: 6, 2: 6, 3: 8, 4: 10}, 'b': {0: 6, 1: 9, 2: 10, 3: 7, 4: 4}}
e_dict = {'one': {'u_mean': 4.25, 'c_mean': 4.25}, 'three': {'u_mean': 4.5, 'c_mean': 4.5}, 'two': {'u_mean': 4.583333333333334, 'c_mean': 4.583333333333334}, 'four': {'u_mean': 4.5625, 'c_mean': 4.5625}, 'five': {'u_mean': 4.65, 'c_mean': 4.65}}

Code:

import pandas as pd
df = pd.DataFrame(data)
df_e = pd.DataFrame(e_dict).T.rename_axis('name').reset_index()

df = df.merge(df_e, on=['name'])
df = df.groupby(['clu']).agg({'u_mean':'mean', 'c_mean':'mean'})
df.to_dict(orient='index')

Output:

{'e': {'u_mean': 4.53125, 'c_mean': 4.53125},
 'q': {'u_mean': 4.4944444444444445, 'c_mean': 4.4944444444444445}}

Explaination:

Pandas allows to handle data in a tabular formar, similar to the data you showed in the example. The first table (DataFrame) is df, the second one is the table containing the lookup values (e_dict) but a bit preprocessed (Transposed and renamed columns).

Then we merge both tables based on their name values, so that you have the coresponding values of u_mean and c_mean in the first table.

Now we group the values by the clue values and aggregate the values with the mean.

Finally we return the table as a dictionary.

Related