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.
>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