Useful links and snippets regarding Python and Jupyter notebooks


iPython/Jupyter resources

Quick and dirty

  • # py3
    from urllib import parse
    # or in py2.7
    import urllib
  • Translate coordinate reference system from *.prj files to proj4-Syntax as required by QGIS using the gdal python module (via 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: <string with proj4 parameters follows>


  • Invert a single column of a Pandas Dataframe: df.col2 = df.col2.values[::-1] via StackOverflow
  • Joining multiple overlapping column names from multiple dataframes: pd.concat(data, axis=1) via StackOverflow
  • Cross-section indexing of a multi-indexed dataframe - using levels: concatdf.xs('MIndexValue', level='MIndexName') - via Pandas documentation and StackOverflow
  • 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))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 StackOverflow
  • Installing a new module in Enthough Canopy Light's distro on Windows: I managed to install the DBF module by navigating to the unzipped content downloaded in the Python console. Then I just typed %run install and the compiled module is then installed in the Canopy's User\Lib\site-packages directory. Easy.