S02-Accessing A SQLite Database With Pandas
S02-Accessing A SQLite Database With Pandas
ipynb - Colab
This notebook is extracted from another tutorial of pySQLiteSWAT. If you are intested in more python-based tools such as Pandas, GeoPandas,
PySAL, folium, etc., you can have a look.
con = sqlite3.connect('data\\demo.db3')
cursor = con.cursor()
table_columns(con, 'rch')
['RCH',
'YR',
'MO',
'FLOW_INcms',
'FLOW_OUTcms',
'EVAPcms',
'TLOSScms',
'SED_INtons',
'SED_OUTtons',
'SEDCONCmg_kg',
'ORGN_INkg',
'ORGN_OUTkg',
'ORGP_INkg',
'ORGP_OUTkg',
'NO3_INkg',
'NO3_OUTkg',
'NH4_INkg',
'NH4_OUTkg',
'NO2_INkg',
'NO2_OUTkg',
'MINP_INkg',
'MINP_OUTkg',
'CHLA_INkg',
https://colab.research.google.com/drive/1fdQTJC7_Fm-otRsl2LC_Z9WC1Sk2YirY#printMode=true 1/4
7/25/24, 4:14 PM s02-Accessing a SQLite Database with Pandas.ipynb - Colab
'CHLA_OUTkg',
'CBOD_INkg',
'CBOD_OUTkg',
'DISOX_INkg',
'DISOX_OUTkg',
'SOLPST_INmg',
'SOLPST_OUTmg',
'SORPST_INmg',
'SORPST_OUTmg',
'REACTPSTmg',
'VOLPSTmg',
'SETTLPSTmg',
'RESUSP_PSTmg',
'DIFFUSEPSTmg',
'REACBEDPSTmg',
'BURYPSTmg',
'BED_PSTmg',
'BACTP_OUTct',
'BACTLP_OUTct',
'CMETAL_1kg',
'CMETAL_2kg',
'CMETAL_3kg',
'TOT_Nkg',
'TOT_Pkg',
'NO3ConcMg_l',
'WTMPdegc']
keyboard_arrow_down 2.4 Read specified columns of RCH table inot a pandas DataFrame
RCH YR FLOW_OUTcms
MO
1 1 1981 146.252487
1 2 1981 96.182854
1 3 1981 11.861368
1 4 1981 49.406513
1 5 1981 272.106018
1 6 1981 486.318573
1 7 1981 23.289026
1 8 1981 214.281433
1 9 1981 193.491211
1 10 1981 53.856945
con.close()
ssndf = df.groupby(['RCH',quarters])['FLOW_OUTcms'].mean()
ssndf.head(5)
RCH
1 DJF 99.204991
JJA 1405.262298
MAM 559.746932
SON 454.737985
2 DJF 56.328539
Name: FLOW_OUTcms, dtype: float64
https://colab.research.google.com/drive/1fdQTJC7_Fm-otRsl2LC_Z9WC1Sk2YirY#printMode=true 2/4
7/25/24, 4:14 PM s02-Accessing a SQLite Database with Pandas.ipynb - Colab
ssndf = ssndf.reset_index()
ssndf.set_index(['RCH'])
ssndf.head(5)
0 1 DJF 99.204991
1 1 JJA 1405.262298
2 1 MAM 559.746932
3 1 SON 454.737985
4 2 DJF 56.328539
0 1 DJF 99.204991
1 1 JJA 1405.262298
2 1 MAM 559.746932
3 1 SON 454.737985
4 2 DJF 56.328539
RCH
keyboard_arrow_down 4. Visualize
Set some parameters to make figure pretty
https://colab.research.google.com/drive/1fdQTJC7_Fm-otRsl2LC_Z9WC1Sk2YirY#printMode=true 3/4
7/25/24, 4:14 PM s02-Accessing a SQLite Database with Pandas.ipynb - Colab
<matplotlib.axes._subplots.AxesSubplot at 0xbed9128>
keyboard_arrow_down References
Fernando Pérez and Brian E. Granger. IPython: A System for Interactive Scientific Computing, Computing in Science & Engineering, 9, 21-29
(2007), DOI:10.1109/MCSE.2007.53
John D. Hunter. Matplotlib: A 2D Graphics Environment, Computing in Science & Engineering, 9, 90-95 (2007), DOI:10.1109/MCSE.2007.55
Wes McKinney. Data Structures for Statistical Computing in Python, Proceedings of the 9th Python in Science Conference, 51-56 (2010)
https://www.sqlite.org/lang.html
https://colab.research.google.com/drive/1fdQTJC7_Fm-otRsl2LC_Z9WC1Sk2YirY#printMode=true 4/4