Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to group the JSON objects based on attribute using pandas

I have to group the json object based on some date attribute in it. My JSON will look like below

{
  "myroot": [{
              "attribute1": 10,
              "date":"2023-08-01",
                },
                {
                  "attribute1": 5,
              "date":"2023-08-02",
                },
                {
                  "attribute1": 100,
              "date":"2023-08-02",
                },
                {
                  "attribute1": 2,
              "date":"2023-08-01",
                }]
}

What I want to achieve is to find the sum of attribute1 on date wise. How to do it in python. I am very new to Pandas and NumPy. Thanks in advance
Note: I shouldn’t change the JSON structure.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The output will have the ‘date’ and the sum of ‘attribute1’ for each one

import pandas as pd
    import json
    
    json_data = '''
    {
        "myroot": [
            {"attribute1": 10, "date": "2023-08-01"},
            {"attribute1": 5, "date": "2023-08-02"},
            {"attribute1": 100, "date": "2023-08-02"},
            {"attribute1": 2, "date": "2023-08-01"}
        ]
    }
    '''
    
    # Load JSON data
    data = json.loads(json_data)
    
    # Convert the 'myroot' list of dictionaries into a pandas DataFrame
    df = pd.DataFrame(data['myroot'])
    
    # Convert the 'date' column to datetime type
    df['date'] = pd.to_datetime(df['date'])
    
    # Group by 'date' and calculate the sum of 'attribute1'
    result = df.groupby('date')['attribute1'].sum().reset_index()
    
    print(result)

output will looks like

  date          attribute1
0 2023-08-01          12
1 2023-08-02         105
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading