Home > Software design >  Need help in combining output of a python function
Need help in combining output of a python function

Time:06-02

I have output to a python function:

"top_duration": [
{
"duration": 3752,
"image_name": "ohcm-designer-orchestration-service"
},
{
"duration": 3471,
"image_name": "ohcm-designer-orchestration-service"
},
{
"duration": 3452,
"image_name": "metadata-validations-service"
},
{
"duration": 120,
"image_name": "metadata-validations-service"
},
{
"duration": 114,
"image_name": "metadata-validations-service"
},
{
"duration": 111,
"image_name": "ohcm-metadata-dna-service"
},
{
"duration": 102,
"image_name": "metadata-validations-service"
},
{
"duration": 100,
"image_name": "metadata-runtime-proxy-service"
},

I want to convert the above into the format like {Sum of Duration, image_name}

For e.g. output: {3452 120 114 102, "metadata-validations-service"} Final output: {3788, "metadata-validations-service"}

What approach can I take to achieve this?

CodePudding user response:

from collections import defaultdict

dict_data = {"top_duration": [
    {
        "duration": 3752,
        "image_name": "ohcm-designer-orchestration-service"
    },
    {
        "duration": 3471,
        "image_name": "ohcm-designer-orchestration-service"
    },
    {
        "duration": 3452,
        "image_name": "metadata-validations-service"
    },
    {
        "duration": 120,
        "image_name": "metadata-validations-service"
    },
    {
        "duration": 114,
        "image_name": "metadata-validations-service"
    },
    {
        "duration": 111,
        "image_name": "ohcm-metadata-dna-service"
    },
    {
        "duration": 102,
        "image_name": "metadata-validations-service"
    },
    {
        "duration": 100,
        "image_name": "metadata-runtime-proxy-service"
    }]}


final_dict_data = defaultdict(int)
for item in dict_data['top_duration']:
    final_dict_data[item['image_name']]  = item['duration']
    
final_dict_data = {val:key for key,val in final_dict_data.items()}      
{7223: 'ohcm-designer-orchestration-service',
 3788: 'metadata-validations-service',
 111: 'ohcm-metadata-dna-service',
 100: 'metadata-runtime-proxy-service'}

CodePudding user response:

One way to do it, is using pandas:

import json
import pandas as pd
from pandas import json_normalize
data = json.dumps({
"top_duration": [
{
"duration": 3752,
"image_name": "ohcm-designer-orchestration-service"
},
{
"duration": 3471,
"image_name": "ohcm-designer-orchestration-service"
},
{
"duration": 3452,
"image_name": "metadata-validations-service"
},
{
"duration": 120,
"image_name": "metadata-validations-service"
},
{
"duration": 114,
"image_name": "metadata-validations-service"
},
{
"duration": 111,
"image_name": "ohcm-metadata-dna-service"
},
{
"duration": 102,
"image_name": "metadata-validations-service"
},
{
"duration": 100,
"image_name": "metadata-runtime-proxy-service"
}]
})

file = json.loads(data)
df2 = json_normalize(file['top_duration'])

new_df = df2.groupby(['image_name'])['duration'].sum().reset_index()

output:

                            image_name  duration
0       metadata-runtime-proxy-service       100
1         metadata-validations-service      3788
2  ohcm-designer-orchestration-service      7223
3            ohcm-metadata-dna-service       111
  • Related