Home > front end >  How convert multiple keys into single of dictionary and create Dictionary with multiple values per k
How convert multiple keys into single of dictionary and create Dictionary with multiple values per k

Time:05-04

I have nested dict. like this I am getting this data from Api ,this is nothing but data_response:

    {
    "Parameter_1": {
        "2021-11-16 14:29:00": 319.56,
        "2021-11-16 15:16:00": 319.56,
        "2021-11-16 15:17:00": 319.56,
        "2021-11-17 00:00:00": 335.48,
        "2021-11-17 00:01:00": 335.48,
        "2021-11-17 00:02:00": 335.48,
        "2021-11-18 00:00:00": 355.45,
        "2021-11-18 00:01:00": 355.45,
        "2021-11-18 00:03:00": 355.45,
},
    "Parameter_2": {
        "2021-11-16 14:29:00": 319.56,
        "2021-11-16 15:16:00": 319.56,
        "2021-11-16 15:17:00": 319.56,
        "2021-11-17 00:00:00": 335.48,
        "2021-11-17 00:01:00": 335.48,
        "2021-11-17 00:02:00": 335.48,
         "2021-11-18 00:00:00": 355.45,
        "2021-11-18 00:01:00": 355.45,
        "2021-11-18 00:03:00": 355.45,
}

This is a code I does:

data = []
for parameter in parameters:
    for key, values in data_response.items():
        for key1, value in values.items():
            date = key1.split(" ")[0]

    value_list = [value for key1, value in values.items()]
    data.append(value_list)

Output I am getting in data is only values :

Data :[[319.56,319.56, 319.56, 335.48,335.48,335.48,355.45,355.45,355.45]]

I wish to get values of key ,with only single date. Expected output is:

Output Expected 1:

{
    "Parameter_1": {
        "2021-11-16: [319.56,319.56,319.56],
        "2021-11-17": [335.48,335.48,335.48],
        "2021-11-18": [355.45,355.45,355.45],
},
    "Parameter_2": {
        "2021-11-16": [319.56,319.56, 319.56],
        "2021-11-17": [335.48,335.48,335.48],
        "2021-11-18": [355.45,355.45,355.45],
}

Output Expected 2:

Date:
     [
      parameter1[2021-11-16][2021-11-17][2021-11-18],
      parameter2[2021-11-16][2021-11-17][2021-11-18]
     ]
Values:
     [
      parameter1[319.56,319.56,319.56][335.48,335.48,335.48][355.45,355.45,355.45],
      parameter2[319.56,319.56,319.56][335.48,335.48,335.48][355.45,355.45,355.45]
     ]

Can you please help me to achieve my output ?

CodePudding user response:

I think this is what you're looking for:

d = {
    "Parameter_1": {
        "2021-11-16 14:29:00": 319.56,
        "2021-11-16 15:16:00": 319.56,
        "2021-11-16 15:17:00": 319.56,
        "2021-11-17 00:00:00": 335.48,
        "2021-11-17 00:01:00": 335.48,
        "2021-11-17 00:02:00": 335.48,
        "2021-11-18 00:00:00": 355.45,
        "2021-11-18 00:01:00": 355.45,
        "2021-11-18 00:03:00": 355.45,
},
    "Parameter_2": {
        "2021-11-16 14:29:00": 319.56,
        "2021-11-16 15:16:00": 319.56,
        "2021-11-16 15:17:00": 319.56,
        "2021-11-17 00:00:00": 335.48,
        "2021-11-17 00:01:00": 335.48,
        "2021-11-17 00:02:00": 335.48,
         "2021-11-18 00:00:00": 355.45,
        "2021-11-18 00:01:00": 355.45,
        "2021-11-18 00:03:00": 355.45,
}}

r = dict()

for k, v in d.items():
    r[k] = dict()
    for k_, v_ in v.items():
        r[k].setdefault(k_[:10], []).append(v_)

print(r)

Output:

{'Parameter_1': {'2021-11-16': [319.56, 319.56, 319.56], '2021-11-17': [335.48, 335.48, 335.48], '2021-11-18': [355.45, 355.45, 355.45]}, 'Parameter_2': {'2021-11-16': [319.56, 319.56, 319.56], '2021-11-17': [335.48, 335.48, 335.48], '2021-11-18': [355.45, 355.45, 355.45]}}

CodePudding user response:

This is pretty clean data, and Pandas would handle it really well:

import pandas as pd

data = {
    "Parameter_1": {
        "2021-11-16 14:29:00": 319.56,
        "2021-11-16 15:16:00": 319.56,
        "2021-11-16 15:17:00": 319.56,
        "2021-11-17 00:00:00": 335.48,
        "2021-11-17 00:01:00": 335.48,
        "2021-11-17 00:02:00": 335.48,
        "2021-11-18 00:00:00": 355.45,
        "2021-11-18 00:01:00": 355.45,
        "2021-11-18 00:03:00": 355.45,
},
    "Parameter_2": {
        "2021-11-16 14:29:00": 319.56,
        "2021-11-16 15:16:00": 319.56,
        "2021-11-16 15:17:00": 319.56,
        "2021-11-17 00:00:00": 335.48,
        "2021-11-17 00:01:00": 335.48,
        "2021-11-17 00:02:00": 335.48,
         "2021-11-18 00:00:00": 355.45,
        "2021-11-18 00:01:00": 355.45,
        "2021-11-18 00:03:00": 355.45,
}

df = pd.DataFrame(data).reset_index()
df['index'] = pd.to_datetime(df['index']).dt.date.astype(str)
output = df.groupby('index').agg(list).to_dict()
print(output)

Output: (Formatting added manually)

{
    'Parameter_1': {
        '2021-11-16': [319.56, 319.56, 319.56], 
        '2021-11-17': [335.48, 335.48, 335.48], 
        '2021-11-18': [355.45, 355.45, 355.45]
    },
    'Parameter_2': {
        '2021-11-16': [319.56, 319.56, 319.56], 
        '2021-11-17': [335.48, 335.48, 335.48], 
        '2021-11-18': [355.45, 355.45, 355.45]
    }
}
  • Related