Useful links and snippets regarding Python and Jupyter notebooks
====== Modules ======
* The uncompromising Python code formatter [[https://black.readthedocs.io/en/stable/]] and on Github: [[https://github.com/psf/black]]
===== iPython/Jupyter resources =====
* [[https://www.packtpub.com/books/content/basics-jupyter-notebook-and-python|Basics of Jupyter notebooks (on packtpub.com)]]
* [[http://maxmelnick.com/2016/04/19/python-beginner-tips-and-tricks.html|Max Melnick's Jupyter Python Notebook Keyboard Shortcuts and Text Snippets for Beginners]]
* [[http://jupyter.readthedocs.io/en/latest/index.html|Jupyter notebooks documentation]]
* [[http://matplotlib.org/examples/color/colormaps_reference.html|Matplotlib colormaps]] and [[https://stackoverflow.com/questions/3373256/set-colorbar-range-in-matplotlib| how to normalise a colormap]]
* [[https://maxmasnick.com/projects/scipy-tips/|Max Masnick's Dat science Python snippets]]
* [[https://realpython.com/python-matplotlib-guide/|MatPlotLib guide]]
===== Quick and dirty =====
* [[http://stackoverflow.com/questions/16566069/url-decode-utf-8-in-python#16566128|Decode URLs using Python]]
*
# py3
from urllib import parse
parse.unquote("""TheURLString""")
# or in py2.7
import urllib
urllib.unquote("theURL").decode('utf8')
* Translate coordinate reference system from ''*.prj'' files to proj4-Syntax as required by QGIS using the gdal python module (via [[https://gis.stackexchange.com/questions/7608/shapefile-prj-to-postgis-srid-lookup-table/7615#7615|GIS on Stackexchange]]:
*
import sys
from osgeo import osr
prjtxt = """PROJCS["your_crs",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["the_prj"],PARAMETER["Central_Meridian",0.0],PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",0.0],PARAMETER["Latitude_Of_Origin",0.0],PARAMETER["Standard_Parallel_1",-10],PARAMETER["Standard_Parallel_2",50],UNIT["Meter",1.0],AUTHORITY["me",123456]]"""
srs = osr.SpatialReference()
print 'Proj4 is: %s' % srs.ExportToProj4()
Proj4 is:
==== Pandas/Geopandas ====
* Invert a single column of a Pandas Dataframe: ''df.col2 = df.col2.values[::-1]'' [[https://stackoverflow.com/questions/46244235/invert-a-single-column-in-a-dataframe|via StackOverflow]]
* Joining multiple overlapping column names from multiple dataframes: ''pd.concat(data, axis=1)'' [[https://stackoverflow.com/questions/13003769/joining-multiple-dataframes-with-pandas-with-overlapping-column-names|via StackOverflow]]
* Cross-section indexing of a multi-indexed dataframe - using levels: ''concatdf.xs('MIndexValue', level='MIndexName')'' - [[http://pandas-docs.github.io/pandas-docs-travis/user_guide/advanced.html#advanced-xs|via Pandas documentation]] and [[https://stackoverflow.com/questions/24435788/using-loc-with-a-multiindex-in-pandas#24436783|StackOverflow]]
* [[https://stackoverflow.com/questions/53645882/pandas-merging-101|Pandas merging 101]] on StackOverflow.
* Going from a regular dataframe to geopandas data frame: ''gdf = geopandas.GeoDataFrame(df, geometry=geopandas.points_from_xy(df.Longitude, df.Latitude))'' -- [[https://geopandas.readthedocs.io/en/latest/gallery/create_geopandas_from_pandas.html|via Geopandas doc]]
* Obtaining unique values for Multi-index levels: ''concatdf.index.unique(level=1)''
* Drop a level of multi-index into a column: ''df.reset_index(level=[...])'' - via [[https://stackoverflow.com/questions/20110170/turn-pandas-multi-index-into-column|StackOverflow]]
* Generating a column with conditional values based on an existing column [[https://datagy.io/pandas-conditional-column/|via datagy.io]]: ''myDict = { 'a': 1, 'b': 2, 'c': 3}'', ''df[newCol] = df[oldCol].map( myDict )''
===== Stuff related to modules =====
* [[https://gdal.org/python/osgeo.ogr-module.html|osgeo.ogr]]
* [[http://effbot.org/zone/import-confusion.htm|Importing modules - effbot's import confusion]]
* Installing a new module in Enthough Canopy Light's distro on Windows: I managed to install the [[https://pypi.python.org/pypi/dbf|DBF module]] by navigating to the unzipped content downloaded in the Python console. Then I just typed ''%run setup.py install'' and the compiled module is then installed in the Canopy's ''User\Lib\site-packages'' directory. Easy.
{{tag> Python Programming Jupyter iPython notebooks pandas}}