API tutorial 4 - Process query result data and save to file¶
Repository: https://github.com/openego/oedialect
Please report bugs and improvements here: https://github.com/OpenEnergyPlatform/examples/issues
How to get started with Jupyter Notebooks can be found here: https://realpython.com/jupyter-notebook-introduction/
Please ensure you have read the Terms of use here: https://openenergy-platform.org/legal/tou/
In [1]:
Copied!
__copyright__ = "Zentrum für nachhaltige Energiesysteme Flensburg"
__license__ = "GNU Affero General Public License Version 3 (AGPL-3.0)"
__url__ = "https://github.com/openego/data_processing/blob/master/LICENSE"
__author__ = "wolfbunke"
__copyright__ = "Zentrum für nachhaltige Energiesysteme Flensburg"
__license__ = "GNU Affero General Public License Version 3 (AGPL-3.0)"
__url__ = "https://github.com/openego/data_processing/blob/master/LICENSE"
__author__ = "wolfbunke"
Introduction¶
This is an important information!
This is an information!
This is your task!
This tutorial gives you an overview of the OpenEnergy Platform and how you can work with the REST-full-HTTP API in Python.
The full API documentaion can be found on ReadtheDocs.io.
Part IV - How to work with the OpenEnergy Platform (OEP)¶
0 Setup token
1 Select data
2 Make a pandas dataframe
3 Make calculations
4 Save results as csv and excel files
In [ ]:
Copied!
import requests
import pandas as pd
from IPython.core.display import HTML
from token_config import oep_url, get_oep_token
# token
your_token = get_oep_token()
import requests
import pandas as pd
from IPython.core.display import HTML
from token_config import oep_url, get_oep_token
# token
your_token = get_oep_token()
1. Select data¶
In [ ]:
Copied!
# select powerplant data
schema = 'supply'
table = 'ego_dp_conv_powerplant'
where = 'version=v0.2.10'
conv_powerplants = requests.get(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/rows/?where='+where, )
conv_powerplants.status_code
# select powerplant data
schema = 'supply'
table = 'ego_dp_conv_powerplant'
where = 'version=v0.2.10'
conv_powerplants = requests.get(oep_url+'/api/v0/schema/'+schema+'/tables/'+table+'/rows/?where='+where, )
conv_powerplants.status_code
Response [200] succesfully selected data!
Response [404] table doesn't exist!
Response [404] table doesn't exist!
2. Make a pandas dataframe¶
In [ ]:
Copied!
df_pp = pd.DataFrame(conv_powerplants.json())
df_pp = pd.DataFrame(conv_powerplants.json())
3. Make calculations¶
Get an overview of your DataFrame:
In [ ]:
Copied!
df_pp.info()
df_pp.info()
Sum the installed Capacity by fuels and add the unit MW in a new column.
In [ ]:
Copied!
results = df_pp[['capacity','fuel']].groupby('fuel').sum()
results['units'] = 'MW'
results
results = df_pp[['capacity','fuel']].groupby('fuel').sum()
results['units'] = 'MW'
results
Create a csv file from the data frame with a suitable name and configure the representation of the different data types.
In [ ]:
Copied!
# Write DataFrame as csv with desired column seperator and representation of the datatypes
results.to_csv(
'Conventional_powerplants_germany.csv',
sep=',',
float_format='%.3f',
decimal='.',
date_format='%Y-%m-%d',
encoding='utf-8'
)
# Write DataFrame as csv with desired column seperator and representation of the datatypes
results.to_csv(
'Conventional_powerplants_germany.csv',
sep=',',
float_format='%.3f',
decimal='.',
date_format='%Y-%m-%d',
encoding='utf-8'
)
The file can be found in the /api folder.
Create a .xlsx file with a suitable name and two sheets. The sheets must contain the data from 1. result and 2. df_pp.
In [ ]:
Copied!
# Write the results as xlsx file
writer = pd.ExcelWriter('Conventional_powerplants_germany.xlsx', engine='xlsxwriter')
# write results of installed Capacity by fuels
results.to_excel(writer, index=False, sheet_name='Installed Capacities by fuel')
# write orgininal data in second sheet
df_pp.to_excel(writer, index=False, sheet_name='Conventional Powerplants')
# Close the Pandas Excel writer and output the Excel file.
writer.save()
# Write the results as xlsx file
writer = pd.ExcelWriter('Conventional_powerplants_germany.xlsx', engine='xlsxwriter')
# write results of installed Capacity by fuels
results.to_excel(writer, index=False, sheet_name='Installed Capacities by fuel')
# write orgininal data in second sheet
df_pp.to_excel(writer, index=False, sheet_name='Conventional Powerplants')
# Close the Pandas Excel writer and output the Excel file.
writer.save()
The file can be found in the /api folder.