How to execute function as a json in python?

Viewed 94

I'm a beginner and now I'm trying to do something with django and data science. My project is to rate salesmen for their service and I want to do some magic with data science.)) (showing average ratings of salesmen and etc.) For now, I have this function:

def average_values():
    average_values_in_json = []
    data = json_maker()
    df = pd.read_json(data)
    df = df.set_index('id')
    salesman_group = df.groupby(['salesperson'])
    for person in df['salesperson'].unique():
        salesperson = salesman_group.get_group(f"{person}").mean()
        data = {
            "salesperson": person,
            "average": salesperson['rating']
        }
        average_values_in_json.append(data)
    average_values_in_json = json.dumps(average_values_in_json)
    return average_values_in_json

It works nice, I rendered it properly and now I want to represent graph with matplotlib. In my views.py, I have:

def graph_show(request):
    new_data  = average_values()
    x = [new_data[i]['salesperson'] for i in range(len(new_data))]
    y = [new_data[i]['average'] for i in range(len(new_data))]
    chart = get_plot(x,y)
    return render(request,
                  'report/graph.html',
                  {'x': x,
                   'y': y})

The problem is that new_data takes string type of the return of function. But I need a json object in order to my further codes work properly. What kind of advice would you give?

0 Answers
Related