I am using an API pull to extract data from the AESO API in python. My code is as follows:
API_KEY = 'api_key_here'
merit_order_url = 'https://api.aeso.ca/report/v1/meteredvolume/details?startDate=2022-01-01'
url = merit_order_url
headers = {'accept': 'application/json', 'X-API-Key': API_KEY}
response = requests.get(url, headers=headers)
The JSON response looks something like this:
{'timestamp': '2023-08-10 14:07:24.976+0000',
'responseCode': '200',
'return': [{'pool_participant_ID': '9496',
'asset_list': [{'asset_ID': '941A',
'asset_class': 'RETAILER',
'metered_volume_list': [{'begin_date_utc': '2022-01-01 07:00',
'begin_date_mpt': '2022-01-01 00:00',
'metered_volume': '0.0005865'},
{'begin_date_utc': '2022-01-01 08:00',
'begin_date_mpt': '2022-01-01 01:00',
'metered_volume': '0.0005363'},
{'begin_date_utc': '2022-01-01 09:00',
'begin_date_mpt': '2022-01-01 02:00',
'metered_volume': '0.0005209'},
{'begin_date_utc': '2022-01-01 10:00',
'begin_date_mpt': '2022-01-01 03:00',
'metered_volume': '0.0005171'},
{'begin_date_utc': '2022-01-01 11:00',
'begin_date_mpt': '2022-01-01 04:00',
'metered_volume': '0.0005152'},
{'begin_date_utc': '2022-01-01 12:00',
'begin_date_mpt': '2022-01-01 05:00',
'metered_volume': '0.0005104'},
{'begin_date_utc': '2022-01-01 13:00',
'begin_date_mpt': '2022-01-01 06:00',
'metered_volume': '0.0005164'},
{'begin_date_utc': '2022-01-01 14:00',
'begin_date_mpt': '2022-01-01 07:00',
'metered_volume': '0.0005426'},
{'begin_date_utc': '2022-01-01 15:00',
'begin_date_mpt': '2022-01-01 08:00',
'metered_volume': '0.0005907'},
{'begin_date_utc': '2022-01-01 16:00',
'begin_date_mpt': '2022-01-01 09:00',
'metered_volume': '0.0006283'},
{'begin_date_utc': '2022-01-01 17:00',
'begin_date_mpt': '2022-01-01 10:00',
'metered_volume': '0.0006528'},
{'begin_date_utc': '2022-01-01 18:00',
'begin_date_mpt': '2022-01-01 11:00',
'metered_volume': '0.0007141'},
{'begin_date_utc': '2022-01-01 19:00',
'begin_date_mpt': '2022-01-01 12:00',
'metered_volume': '0.0007192'},
{'begin_date_utc': '2022-01-01 20:00',
'begin_date_mpt': '2022-01-01 13:00',
'metered_volume': '0.0007495'},
{'begin_date_utc': '2022-01-01 21:00',
'begin_date_mpt': '2022-01-01 14:00',
'metered_volume': '0.0006842'},
{'begin_date_utc': '2022-01-01 22:00',
'begin_date_mpt': '2022-01-01 15:00',
'metered_volume': '0.0006804'},
{'begin_date_utc': '2022-01-01 23:00',
'begin_date_mpt': '2022-01-01 16:00',
'metered_volume': '0.0007282'},
{'begin_date_utc': '2022-01-02 00:00',
'begin_date_mpt': '2022-01-01 17:00',
'metered_volume': '0.0008322'},
{'begin_date_utc': '2022-01-02 01:00',
'begin_date_mpt': '2022-01-01 18:00',
'metered_volume': '0.0008516'},
{'begin_date_utc': '2022-01-02 02:00',
'begin_date_mpt': '2022-01-01 19:00',
'metered_volume': '0.0007729'},
{'begin_date_utc': '2022-01-02 03:00',
'begin_date_mpt': '2022-01-01 20:00',
'metered_volume': '0.0006861'},
{'begin_date_utc': '2022-01-02 04:00',
'begin_date_mpt': '2022-01-01 21:00',
'metered_volume': '0.0006861'},
{'begin_date_utc': '2022-01-02 05:00',
'begin_date_mpt': '2022-01-01 22:00',
'metered_volume': '0.0006434'},
{'begin_date_utc': '2022-01-02 06:00',
'begin_date_mpt': '2022-01-01 23:00',
'metered_volume': '0.0005783'}]},
{'asset_ID': '941C',
'asset_class': 'RETAILER',
'metered_volume_list': [{'begin_date_utc': '2022-01-01 07:00',
'begin_date_mpt': '2022-01-01 00:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-01 08:00',
'begin_date_mpt': '2022-01-01 01:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-01 09:00',
'begin_date_mpt': '2022-01-01 02:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-01 10:00',
'begin_date_mpt': '2022-01-01 03:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-01 11:00',
'begin_date_mpt': '2022-01-01 04:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-01 12:00',
'begin_date_mpt': '2022-01-01 05:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-01 13:00',
'begin_date_mpt': '2022-01-01 06:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-01 14:00',
'begin_date_mpt': '2022-01-01 07:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-01 15:00',
'begin_date_mpt': '2022-01-01 08:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-01 16:00',
'begin_date_mpt': '2022-01-01 09:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-01 17:00',
'begin_date_mpt': '2022-01-01 10:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-01 18:00',
'begin_date_mpt': '2022-01-01 11:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-01 19:00',
'begin_date_mpt': '2022-01-01 12:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-01 20:00',
'begin_date_mpt': '2022-01-01 13:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-01 21:00',
'begin_date_mpt': '2022-01-01 14:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-01 22:00',
'begin_date_mpt': '2022-01-01 15:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-01 23:00',
'begin_date_mpt': '2022-01-01 16:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-02 00:00',
'begin_date_mpt': '2022-01-01 17:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-02 01:00',
'begin_date_mpt': '2022-01-01 18:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-02 02:00',
'begin_date_mpt': '2022-01-01 19:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-02 03:00',
'begin_date_mpt': '2022-01-01 20:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-02 04:00',
'begin_date_mpt': '2022-01-01 21:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-02 05:00',
'begin_date_mpt': '2022-01-01 22:00',
'metered_volume': '0'},
{'begin_date_utc': '2022-01-02 06:00',
'begin_date_mpt': '2022-01-01 23:00',
'metered_volume': '0'}]},
When I use the following code:
df1 = pd.json_normalize(df['return'])
The dataset looks like the following:
I would like to convert the asset_list column into its own dataframe. Where asset_ID
, asset_class
, begin_date_utc
, begin_date_mpt and
metered_volume` are column. How would I go about this?
2
Answers
you can try this. Use
explode
for the list and usejson_normalize
again on the nested object.Using json_normalize() You need to map the levels in meta and record_path:
Code:
Output: