Pandas: Difference between revisions

From Wildsong
Jump to navigationJump to search
Brian Wilson (talk | contribs)
Brian Wilson (talk | contribs)
Line 216: Line 216:
=== Installation ===
=== Installation ===


I have an OSGEO env so I added Geopandas to it. I said "conda install geopandas" and it said
Apparently there is a problem using the geopandas package and its dependencies from conda-forge.
Long form, problem with fiona: https://github.com/geopandas/geopandas/issues/989
Short answer: '''disable conda-forge in .condarc and start again.'''


  attrs              conda-forge/noarch::attrs-20.3.0-pyhd3deb0d_0
conda remote -n geopandas --all
  boost-cpp          conda-forge/win-64::boost-cpp-1.74.0-h54f0996_2
conda create -n geopandas python pip autopep8 geopandas matplotlib
  bzip2              conda-forge/win-64::bzip2-1.0.8-h8ffe710_4
conda activate geopandas
  cairo              conda-forge/win-64::cairo-1.16.0-hb19e0ff_1008
  cfitsio            conda-forge/win-64::cfitsio-3.470-h0af3d06_7
  click              conda-forge/noarch::click-7.1.2-pyh9f0ad1d_0
  click-plugins      conda-forge/noarch::click-plugins-1.1.1-py_0
  cligj              conda-forge/noarch::cligj-0.7.1-pyhd8ed1ab_0
  curl              conda-forge/win-64::curl-7.75.0-hf1763fc_0
  expat              conda-forge/win-64::expat-2.2.10-h39d44d4_0
  fiona              conda-forge/win-64::fiona-1.8.18-py36hdce5812_1
  fontconfig        conda-forge/win-64::fontconfig-2.13.1-h1989441_1004
  freetype          conda-forge/win-64::freetype-2.10.4-h546665d_1
  freexl            conda-forge/win-64::freexl-1.0.6-ha8e266a_0
  gdal              conda-forge/win-64::gdal-3.2.2-py36hfaf0fdc_0
  geopandas         conda-forge/noarch::geopandas-0.9.0-pyhd8ed1ab_0
  geos              conda-forge/win-64::geos-3.9.1-h39d44d4_2
  geotiff            conda-forge/win-64::geotiff-1.6.0-h8e90983_5
  librttopo          conda-forge/win-64::librttopo-1.1.0-hb340de5_6
  libspatialindex    conda-forge/win-64::libspatialindex-1.9.3-h39d44d4_3
  libspatialite      conda-forge/win-64::libspatialite-5.0.1-h6b539a6_4
  libssh2            conda-forge/win-64::libssh2-1.9.0-h680486a_6
  libtiff            conda-forge/win-64::libtiff-4.2.0-hc10be44_0
  libwebp-base      conda-forge/win-64::libwebp-base-1.2.0-h8ffe710_2
  libxml2            conda-forge/win-64::libxml2-2.9.10-hf5bbc77_3
  lz4-c              conda-forge/win-64::lz4-c-1.9.3-h8ffe710_0
  mkl                conda-forge/win-64::mkl-2020.4-hb70f87d_311
  munch              conda-forge/noarch::munch-2.5.0-py_0
  numpy              conda-forge/win-64::numpy-1.19.5-py36hd1b969e_1
  openjpeg          conda-forge/win-64::openjpeg-2.4.0-h48faf41_0
  pandas            conda-forge/win-64::pandas-1.1.5-py36he38d939_0
  pcre              conda-forge/win-64::pcre-8.44-ha925a31_0
  pixman            conda-forge/win-64::pixman-0.40.0-h8ffe710_0
  poppler            conda-forge/win-64::poppler-21.03.0-h9ff6ed8_0
  poppler-data      conda-forge/noarch::poppler-data-0.4.10-0
  postgresql        conda-forge/win-64::postgresql-13.1-h0f1a9bc_2
  proj              conda-forge/win-64::proj-8.0.0-h1cfcee9_0
  pyproj            conda-forge/win-64::pyproj-3.0.1-py36h86093ec_1
  python-dateutil    conda-forge/noarch::python-dateutil-2.8.1-py_0
  pytz              conda-forge/noarch::pytz-2021.1-pyhd8ed1ab_0
  rtree              pkgs/main/win-64::rtree-0.9.4-py36h21ff451_1
  shapely            conda-forge/win-64::shapely-1.7.1-py36hd33d9d6_4
  sqlite            conda-forge/win-64::sqlite-3.35.2-h8ffe710_0
  tiledb            conda-forge/win-64::tiledb-2.2.7-hf84e3da_0
  tk                conda-forge/win-64::tk-8.6.10-h8ffe710_1
  xerces-c          conda-forge/win-64::xerces-c-3.2.3-h0e60522_2
  xz                conda-forge/win-64::xz-5.2.5-h62dcd97_1
  zlib              conda-forge/win-64::zlib-1.2.11-h62dcd97_1010
  zstd              conda-forge/win-64::zstd-1.4.9-h6255e5f_0


ZOW. Now I wonder what was in there in the first place before installing geopandas.
I note with "conda list" that it found packages in the esri channel, not sure where exactly it picked that up. So be it.
Lots of good stuff in these dependencies like gdal and fiona and proj and geos.


=== Hello World from GeoPandas ===
=== Hello World from GeoPandas ===
Line 279: Line 234:
And THEN start a Jupyter Notebook in VSCODE. Follow along with someone else's sample. See the Reference section below.
And THEN start a Jupyter Notebook in VSCODE. Follow along with someone else's sample. See the Reference section below.


import geopandas
import pandas as pd
import geopandas
import matplotlib as plt
roads = geopandas.read_file("K:/LISData/roads_county.shp")
roads.head(5)
 
and I glory in this!
 
<pre>
STREET FROMLEFT TOLEFT FROMRIGHT TORIGHT LESN RESN NOTES COMMENTS ST_CHK ... SYMBOL CITY IRISNUM sign_lomil sign_himil FUNCLASSD FUNCLASSM FUNCLASSN Shape_Leng geometry
0 HIGHLANDS LN 33301 33399 33300 33398 None None None VERIFY ADDRESS RANGE GSI ... 18 None 1010.0 0.15 0.29 Rural Local Local 4 960.080746 LINESTRING (7333565.484 887114.321, 7333598.68...
1 HAWKINS RD 90200 90298 90201 90299 None None None None GSI ... 18 None 1020.0 0.19 0.51 Rural Local Local 4 1038.814776 LINESTRING (7336949.000 902144.750, 7336920.40...
2 MAKI RD 0 0 0 0 None None None None None ... 18 None 411.0 0.00 0.93 Rural Local Local 4 325.748159 LINESTRING (7399846.775 921085.451, 7399836.70...
3 LOWER NEHALEM RD 79500 79998 79501 79999 None None None None GSI ... 19 None 1404.0 3.21 5.25 Rural Major Collector Collector 3 6089.149044 LINESTRING (7411398.616 799370.758, 7411406.96...
4 LOWER NEHALEM RD
</pre>
 
and CRIKEY it works, right there in VSCODE, the wonder of it!
 
<img src="roads_geopandas.png


== References ==
== References ==

Revision as of 00:07, 27 March 2021

All this stuff is amazing but hard to wrap my head around because it's more like working with SQL than nice linear procedural programming.

When something is hard for me, I write about it in my Wiki.

This page currently is really all about GeoPandas, for now.

There is also the ArcGIS version and I should write about that here too.

Pandas

This is a Python package for manipulating data that implements dataframes. Dataframes are well amazing. Also extremely well documented elsewhere.

So run along and learn Pandas THEN come back and learn GeoPandas.

Esri "ArcGIS API for Python"

Yeah so you can go install this and learn about it, docs are... difficult.

They have these installed,

 packages in environment at C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3:
#
# Name                    Version                   Build  Channel
appdirs                   1.4.4                      py_0
arcgis                    1.8.3                 py37_1668    esri
arcgispro                 2.7                           0    esri
arcpy                     2.7             py37_arcgispro_26810  [arcgispro]  esri
argon2-cffi               20.1.0           py37he774522_1
asn1crypto                1.4.0                      py_0
async_generator           1.10                       py_0
atomicwrites              1.4.0                      py_0
attrs                     20.2.0                     py_0
backcall                  0.2.0                      py_0
black                     19.10b0                    py_0
blas                      1.0                         mkl
bleach                    3.2.1                      py_0
blinker                   1.4                      py37_0
brotlipy                  0.7.0           py37he774522_1000
cached-property           1.5.2                      py_0
certifi                   2020.6.20                py37_0
cffi                      1.14.3           py37h7a1dbc1_0
cftime                    1.0.0b1                  py37_0    esri
chardet                   3.0.4                 py37_1003
click                     7.1.2                      py_0
colorama                  0.4.3                      py_0
cppzmq                    4.4.1                         2    esri
cryptography              2.8                      py37_0    esri
cycler                    0.10.0                   py37_0
decorator                 4.4.2                      py_0
defusedxml                0.6.0                      py_0
despatch                  0.1.0                    py37_0    esri
entrypoints               0.3                      py37_0
et_xmlfile                1.0.1                   py_1001
fastcache                 1.1.0            py37he774522_0
flake8                    3.8.3                      py_0
freetype                  2.10.1                   vc14_0  [vc14]  esri
future                    0.18.2                   py37_0    esri
gdal                      2.3.3           arcgispro_py37_16713  [arcgispro]  esri
h5py                      2.10.0          py37_arcgispro_9  [arcgispro]  esri
html5lib                  1.1                        py_0
icc_rt                    2019.0.5            arcgispro_0  [arcgispro]  esri
idna                      2.10                       py_0
importlib-metadata        1.7.0                    py37_0
importlib_metadata        1.7.0                         0
iniconfig                 1.0.1                      py_0
intel-openmp              2020.0            arcgispro_166  [arcgispro]  esri
ipykernel                 5.3.4            py37h5ca1d4c_0
ipython                   7.18.1                   py37_0    esri
ipython_genutils          0.2.0                    py37_0
ipywidgets                7.5.1                      py_1
jdcal                     1.4.1                      py_0
jedi                      0.17.2                   py37_0    esri
jinja2                    2.11.2                     py_0
jpeg                      9d                            0    esri
json5                     0.9.4                    py37_0    esri
jsonschema                3.2.0                    py37_1
jupyter_client            6.1.7                      py_0    esri
jupyter_console           6.2.0                      py_2    esri
jupyter_contrib_core      0.3.3                    py37_3    esri
jupyter_contrib_nbextensions 0.5.1                   py37_10    esri
jupyter_core              4.6.3                    py37_2    esri
jupyter_highlight_selected_word 0.2.0                    py37_2    esri
jupyter_latex_envs        1.4.4                    py37_1    esri
jupyter_nbextensions_configurator 0.4.1                    py37_1    esri
jupyterlab                2.2.7                      py_0    esri
jupyterlab_pygments       0.1.1                      py_1    esri
jupyterlab_server         1.2.0                      py_0
keyring                   21.4.0                   py37_0    esri
kiwisolver                1.2.0            py37h74a9793_0
lerc                      2.2                        py_0    esri
libpng                    1.6.37               h2a8f88b_0
libsodium                 1.0.18                        1    esri
libtiff                   4.1.0                         0    esri
libxml2                   2.9.10              arcgispro_0  [arcgispro]  esri
libxslt                   1.1.34               he774522_0
lxml                      4.5.2            py37h1350720_0
lz4-c                     1.9.2                hf4a77e7_3
markupsafe                1.1.1            py37hfa6e2cd_1
matplotlib                3.3.1           py37_arcgispro_0  [arcgispro]  esri
mccabe                    0.6.1                    py37_1
mistune                   0.8.4           py37hfa6e2cd_1001
mkl                       2020.0            arcgispro_167  [arcgispro]  esri
mkl-service               2.3.0            py37hb782905_0
mkl_fft                   1.2.0            py37h45dec08_0
mkl_random                1.2.0                    py37_0    esri
mpmath                    1.1.0                    py37_0
mypy_extensions           0.4.3                    py37_0
nbclient                  0.5.0                      py_0    esri
nbconvert                 5.6.1                    py37_0    esri
nbformat                  5.0.7                      py_1    esri
nest-asyncio              1.3.2                      py_0    esri
netcdf4                   1.5.4           py37_arcgispro_5  [arcgispro]  esri
networkx                  2.5                      py37_0    esri
nlohmann_json             3.7.0                         1    esri
nose                      1.3.7                 py37_1004
notebook                  5.7.10                   py37_0
ntlm-auth                 1.4.0                      py_0    esri
numexpr                   2.7.1            py37h25d0782_0
numpy                     1.19.1           py37h5510c5b_0
numpy-base                1.19.1           py37ha3acd2a_0
oauthlib                  3.1.0                      py_0
olefile                   0.46                     py37_0
openpyxl                  3.0.5                      py_0
openssl                   1.1.1g                        2    esri
packaging                 20.4                       py_0
pandas                    1.1.1            py37ha925a31_0
pandocfilters             1.4.2                    py37_1
parso                     0.7.0                      py_0
pathspec                  0.7.0                      py_0
pefile                    2019.4.18                  py_0
pickleshare               0.7.5                 py37_1001
pillow-simd               7.1.2                    py37_3    esri
pip                       20.2.2                   py37_0
pluggy                    0.13.1                   py37_0
pro_notebook_integration  2.7                      py37_1    esri
prometheus_client         0.8.0                      py_0    esri
prompt_toolkit            3.0.5                      py_0    esri
psutil                    5.7.2            py37he774522_0
py                        1.9.0                      py_0
pybind11                  2.3.0                         1    esri
pycodestyle               2.6.0                      py_0
pycparser                 2.20                       py_2
pyflakes                  2.2.0                      py_0
pygments                  2.7.0                      py_0    esri
pyjwt                     1.7.1                    py37_0
pyopenssl                 19.1.0                     py_1
pyparsing                 2.4.7                      py_0
pypdf2                    1.26.0                     py_2    esri
pyrsistent                0.17.3           py37he774522_0
pyshp                     2.1.2                      py_0
pysocks                   1.7.1                    py37_1
pytest                    6.1.1                    py37_0    esri
python                    3.7.9                         2    esri
python-certifi-win32      1.2                      py37_0    esri
python-dateutil           2.8.1                      py_0
pytz                      2020.1                   py37_0    esri
pywin32-ctypes            0.2.0                    pypi_0    pypi
pywin32-security          228                      py37_3    esri
pywinpty                  0.5.7                    py37_0    esri
pyyaml                    5.3.1            py37he774522_1
pyzmq                     19.0.2                   py37_1    esri
regex                     2020.7.14        py37he774522_0
requests                  2.24.0                     py_0
requests-kerberos         0.12.0                        0    esri
requests-negotiate-sspi   0.5.2                    py37_1    esri
requests-oauthlib         1.3.0                      py_0
requests-toolbelt         0.9.1                      py_0
requests_ntlm             1.1.0                      py_0    esri
scipy                     1.5.2            py37h9439919_0
send2trash                1.5.0                    py37_0
setuptools                49.6.0                   py37_0
simplegeneric             0.8.1                    py37_2
six                       1.15.0                     py_0
sqlite                    3.33.0               h2a8f88b_0
sympy                     1.5.1                    py37_0    esri
terminado                 0.8.3                    py37_0
testpath                  0.4.4                      py_0
toml                      0.10.1                     py_0
tornado                   6.0.4            py37he774522_1
traitlets                 4.3.3                    py37_0
typed-ast                 1.4.1            py37he774522_0
typing_extensions         3.7.4.3                    py_0
ujson                     3.1.0            py37ha925a31_0
urllib3                   1.25.10                    py_0
vc                        14.1                 h0510ff6_4
vs2015_runtime            14.16.27012          hf0eaf9b_0    esri
wcwidth                   0.2.5                      py_0
webencodings              0.5.1                    py37_1
wheel                     0.35.1                     py_0
widgetsnbextension        3.5.1                    py37_0
win_inet_pton             1.1.0                    py37_0
wincertstore              0.2                      py37_0
winkerberos               0.7.0                    py37_1
winpty                    0.4.3                         4
wrapt                     1.12.1           py37he774522_1
x86cpu                    0.4                      py37_1    esri
xarray                    0.16.0                     py_0
xeus                      0.24.1                        1    esri
xlrd                      1.2.0                      py_0
xlwt                      1.3.0                    py37_0
xtl                       0.6.5                         1    esri
xz                        5.2.5                h62dcd97_0
yaml                      0.2.5                         0    esri
zeromq                    4.3.2                         2    esri
zipp                      3.1.0                      py_0
zlib                      1.2.11               h62dcd97_4
zstd                      1.4.5                h04227a9_0

GeoPandas

GeoPandas is geospatially enabled Pandas. Go right to the page for full info, https://geopandas.org.

Installation

Apparently there is a problem using the geopandas package and its dependencies from conda-forge. Long form, problem with fiona: https://github.com/geopandas/geopandas/issues/989 Short answer: disable conda-forge in .condarc and start again.

conda remote -n geopandas --all
conda create -n geopandas python pip autopep8 geopandas matplotlib
conda activate geopandas

I note with "conda list" that it found packages in the esri channel, not sure where exactly it picked that up. So be it.

Hello World from GeoPandas

In VSCODE, start a Jupyter Notebook. Well, first, install about 50 more packages by doing this,

conda install jupyter jupyterlab

And THEN start a Jupyter Notebook in VSCODE. Follow along with someone else's sample. See the Reference section below.

import pandas as pd
import geopandas
import matplotlib as plt

roads = geopandas.read_file("K:/LISData/roads_county.shp")
roads.head(5)

and I glory in this!

STREET	FROMLEFT	TOLEFT	FROMRIGHT	TORIGHT	LESN	RESN	NOTES	COMMENTS	ST_CHK	...	SYMBOL	CITY	IRISNUM	sign_lomil	sign_himil	FUNCLASSD	FUNCLASSM	FUNCLASSN	Shape_Leng	geometry
0	HIGHLANDS LN	33301	33399	33300	33398	None	None	None	VERIFY ADDRESS RANGE	GSI	...	18	None	1010.0	0.15	0.29	Rural Local	Local	4	960.080746	LINESTRING (7333565.484 887114.321, 7333598.68...
1	HAWKINS RD	90200	90298	90201	90299	None	None	None	None	GSI	...	18	None	1020.0	0.19	0.51	Rural Local	Local	4	1038.814776	LINESTRING (7336949.000 902144.750, 7336920.40...
2	MAKI RD	0	0	0	0	None	None	None	None	None	...	18	None	411.0	0.00	0.93	Rural Local	Local	4	325.748159	LINESTRING (7399846.775 921085.451, 7399836.70...
3	LOWER NEHALEM RD	79500	79998	79501	79999	None	None	None	None	GSI	...	19	None	1404.0	3.21	5.25	Rural Major Collector	Collector	3	6089.149044	LINESTRING (7411398.616 799370.758, 7411406.96...
4	LOWER NEHALEM RD	

and CRIKEY it works, right there in VSCODE, the wonder of it!

<img src="roads_geopandas.png

References

Mastering pandas - Second Edition has a short but sweet chapter on GeoPandas.

Learning Geospatial Analysis with Python - Third Edition has just a brief description of GeoPandas

There are some older references in O'Reilly as well.

This video with Andy Eschbacher has some talk about GeoPandas starting at about 30:00 More tools to look at (ugh): matplotlib, Folium