mirror of
https://github.com/wassname/geopandas.git
synced 2026-09-11 12:10:59 +08:00
351 lines
22 KiB
HTML
351 lines
22 KiB
HTML
|
|
|
|
<!DOCTYPE html>
|
|
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
|
|
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<title>Merging Data — GeoPandas 0.2-py3.5.egg documentation</title>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<link href='https://fonts.googleapis.com/css?family=Lato:400,700,400italic,700italic|Roboto+Slab:400,700|Inconsolata:400,700&subset=latin,cyrillic' rel='stylesheet' type='text/css'>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
|
|
|
|
|
|
|
|
|
|
|
|
<link rel="author" title="About these documents"
|
|
href="about.html"/>
|
|
<link rel="top" title="GeoPandas 0.2-py3.5.egg documentation" href="index.html"/>
|
|
<link rel="next" title="Geocoding" href="geocoding.html"/>
|
|
<link rel="prev" title="Aggregation with dissolve" href="aggregation_with_dissolve.html"/>
|
|
|
|
|
|
<script src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js"></script>
|
|
|
|
</head>
|
|
|
|
<body class="wy-body-for-nav" role="document">
|
|
|
|
<div class="wy-grid-for-nav">
|
|
|
|
|
|
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
|
|
<div class="wy-side-nav-search">
|
|
|
|
<a href="index.html" class="fa fa-home"> GeoPandas</a>
|
|
|
|
|
|
<div role="search">
|
|
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
|
|
<input type="text" name="q" placeholder="Search docs" />
|
|
<input type="hidden" name="check_keywords" value="yes" />
|
|
<input type="hidden" name="area" value="default" />
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
|
|
|
|
|
|
|
|
<ul class="current">
|
|
<li class="toctree-l1"><a class="reference internal" href="install.html">Installation</a><ul>
|
|
<li class="toctree-l2"><a class="reference internal" href="install.html#installing-geopandas">Installing GeoPandas</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="install.html#dependencies">Dependencies</a></li>
|
|
</ul>
|
|
</li>
|
|
<li class="toctree-l1"><a class="reference internal" href="data_structures.html">Data Structures</a><ul>
|
|
<li class="toctree-l2"><a class="reference internal" href="data_structures.html#geoseries">GeoSeries</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="data_structures.html#geodataframe">GeoDataFrame</a></li>
|
|
</ul>
|
|
</li>
|
|
<li class="toctree-l1"><a class="reference internal" href="io.html">Reading and Writing Files</a><ul>
|
|
<li class="toctree-l2"><a class="reference internal" href="io.html#reading-spatial-data">Reading Spatial Data</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="io.html#writing-spatial-data">Writing Spatial Data</a></li>
|
|
</ul>
|
|
</li>
|
|
<li class="toctree-l1"><a class="reference internal" href="mapping.html">Making Maps</a><ul>
|
|
<li class="toctree-l2"><a class="reference internal" href="mapping.html#chloropleth-maps">Chloropleth Maps</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="mapping.html#maps-with-layers">Maps with Layers</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="mapping.html#other-resources">Other Resources</a></li>
|
|
</ul>
|
|
</li>
|
|
<li class="toctree-l1"><a class="reference internal" href="projections.html">Managing Projections</a><ul>
|
|
<li class="toctree-l2"><a class="reference internal" href="projections.html#coordinate-reference-systems">Coordinate Reference Systems</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="projections.html#setting-a-projection">Setting a Projection</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="projections.html#re-projecting">Re-Projecting</a></li>
|
|
</ul>
|
|
</li>
|
|
<li class="toctree-l1"><a class="reference internal" href="geometric_manipulations.html">Geometric Manipulations</a><ul>
|
|
<li class="toctree-l2"><a class="reference internal" href="geometric_manipulations.html#constructive-methods">Constructive Methods</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="geometric_manipulations.html#affine-transformations">Affine transformations</a></li>
|
|
</ul>
|
|
</li>
|
|
<li class="toctree-l1"><a class="reference internal" href="set_operations.html">Set Operations with overlay</a><ul>
|
|
<li class="toctree-l2"><a class="reference internal" href="set_operations.html#the-different-overlay-operations">The different Overlay operations</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="set_operations.html#overlay-countries-example">Overlay Countries Example</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="set_operations.html#more-examples">More Examples</a></li>
|
|
</ul>
|
|
</li>
|
|
<li class="toctree-l1"><a class="reference internal" href="aggregation_with_dissolve.html">Aggregation with dissolve</a><ul>
|
|
<li class="toctree-l2"><a class="reference internal" href="aggregation_with_dissolve.html#dissolve-example"><code class="docutils literal"><span class="pre">dissolve</span></code> Example</a></li>
|
|
</ul>
|
|
</li>
|
|
<li class="toctree-l1 current"><a class="current reference internal" href="">Merging Data</a><ul>
|
|
<li class="toctree-l2"><a class="reference internal" href="#attribute-joins">Attribute Joins</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="#spatial-joins">Spatial Joins</a></li>
|
|
</ul>
|
|
</li>
|
|
<li class="toctree-l1"><a class="reference internal" href="geocoding.html">Geocoding</a></li>
|
|
<li class="toctree-l1"><a class="reference internal" href="reference.html">Reference to All Attributes and Methods</a><ul>
|
|
<li class="toctree-l2"><a class="reference internal" href="reference.html#geodataframe">GeoDataFrame</a></li>
|
|
</ul>
|
|
</li>
|
|
<li class="toctree-l1"><a class="reference internal" href="contributing.html">Contributing to GeoPandas</a><ul>
|
|
<li class="toctree-l2"><a class="reference internal" href="contributing.html#overview">Overview</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="contributing.html#forking-the-geopandas-repository-using-git">1) Forking the <em>geopandas</em> repository using Git</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="contributing.html#creating-a-development-environment">2) Creating a development environment</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="contributing.html#installing-dependencies">3) Installing Dependencies</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="contributing.html#making-a-development-build">4) Making a development build</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="contributing.html#making-changes-and-writing-tests">5) Making changes and writing tests</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="contributing.html#updating-the-documentation">6) Updating the Documentation</a></li>
|
|
<li class="toctree-l2"><a class="reference internal" href="contributing.html#submitting-a-pull-request">7) Submitting a Pull Request</a></li>
|
|
</ul>
|
|
</li>
|
|
<li class="toctree-l1"><a class="reference internal" href="about.html">About</a><ul>
|
|
<li class="toctree-l2"><a class="reference internal" href="about.html#known-issues">Known issues</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
</nav>
|
|
|
|
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
|
|
|
|
|
|
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
|
|
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
|
|
<a href="index.html">GeoPandas</a>
|
|
</nav>
|
|
|
|
|
|
|
|
<div class="wy-nav-content">
|
|
<div class="rst-content">
|
|
<div role="navigation" aria-label="breadcrumbs navigation">
|
|
<ul class="wy-breadcrumbs">
|
|
<li><a href="index.html">Docs</a> »</li>
|
|
|
|
<li>Merging Data</li>
|
|
<li class="wy-breadcrumbs-aside">
|
|
|
|
<a href="_sources/mergingdata.txt" rel="nofollow"> View page source</a>
|
|
|
|
</li>
|
|
</ul>
|
|
<hr/>
|
|
</div>
|
|
<div role="main" class="document">
|
|
|
|
<div class="section" id="merging-data">
|
|
<h1>Merging Data<a class="headerlink" href="#merging-data" title="Permalink to this headline">¶</a></h1>
|
|
<p>There are two ways to combine datasets in <em>geopandas</em> – attribute joins and spatial joins.</p>
|
|
<p>In an attribute join, a <code class="docutils literal"><span class="pre">GeoSeries</span></code> or <code class="docutils literal"><span class="pre">GeoDataFrame</span></code> is combined with a regular <em>pandas</em> <code class="docutils literal"><span class="pre">Series</span></code> or <code class="docutils literal"><span class="pre">DataFrame</span></code> based on a common variable. This is analogous to normal merging or joining in <em>pandas</em>.</p>
|
|
<p>In a Spatial Join, observations from to <code class="docutils literal"><span class="pre">GeoSeries</span></code> or <code class="docutils literal"><span class="pre">GeoDataFrames</span></code> are combined based on their spatial relationship to one another.</p>
|
|
<p>In the following examples, we use these datasets:</p>
|
|
<div class="highlight-ipython"><div class="highlight"><pre><span class="gp">In [1]: </span><span class="n">world</span> <span class="o">=</span> <span class="n">gpd</span><span class="o">.</span><span class="n">read_file</span><span class="p">(</span><span class="n">gpd</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s">'naturalearth_lowres'</span><span class="p">))</span>
|
|
|
|
<span class="gp">In [2]: </span><span class="n">cities</span> <span class="o">=</span> <span class="n">gpd</span><span class="o">.</span><span class="n">read_file</span><span class="p">(</span><span class="n">gpd</span><span class="o">.</span><span class="n">datasets</span><span class="o">.</span><span class="n">get_path</span><span class="p">(</span><span class="s">'naturalearth_cities'</span><span class="p">))</span>
|
|
|
|
<span class="go"># For attribute join</span>
|
|
<span class="gp">In [3]: </span><span class="n">country_shapes</span> <span class="o">=</span> <span class="n">world</span><span class="p">[[</span><span class="s">'geometry'</span><span class="p">,</span> <span class="s">'iso_a3'</span><span class="p">]]</span>
|
|
|
|
<span class="gp">In [4]: </span><span class="n">country_names</span> <span class="o">=</span> <span class="n">world</span><span class="p">[[</span><span class="s">'name'</span><span class="p">,</span> <span class="s">'iso_a3'</span><span class="p">]]</span>
|
|
|
|
<span class="go"># For spatial join</span>
|
|
<span class="gp">In [5]: </span><span class="n">countries</span> <span class="o">=</span> <span class="n">world</span><span class="p">[[</span><span class="s">'geometry'</span><span class="p">,</span> <span class="s">'name'</span><span class="p">]]</span>
|
|
|
|
<span class="gp">In [6]: </span><span class="n">countries</span> <span class="o">=</span> <span class="n">countries</span><span class="o">.</span><span class="n">rename</span><span class="p">(</span><span class="n">columns</span><span class="o">=</span><span class="p">{</span><span class="s">'name'</span><span class="p">:</span><span class="s">'country'</span><span class="p">})</span>
|
|
</pre></div>
|
|
</div>
|
|
<div class="section" id="attribute-joins">
|
|
<h2>Attribute Joins<a class="headerlink" href="#attribute-joins" title="Permalink to this headline">¶</a></h2>
|
|
<p>Attribute joins are accomplished using the <code class="docutils literal"><span class="pre">merge</span></code> method. In general, it is recommended to use the <code class="docutils literal"><span class="pre">merge</span></code> method called from the spatial dataset. With that said, the stand-alone <code class="docutils literal"><span class="pre">merge</span></code> function will work if the GeoDataFrame is in the <code class="docutils literal"><span class="pre">left</span></code> argument; if a DataFrame is in the <code class="docutils literal"><span class="pre">left</span></code> argument and a GeoDataFrame is in the <code class="docutils literal"><span class="pre">right</span></code> position, the result will no longer be a GeoDataFrame.</p>
|
|
<p>For example, consider the following merge that adds full names to a <code class="docutils literal"><span class="pre">GeoDataFrame</span></code> that initially has only ISO codes for each country by merging it with a <em>pandas</em> <code class="docutils literal"><span class="pre">DataFrame</span></code>.</p>
|
|
<div class="highlight-ipython"><div class="highlight"><pre><span class="go"># `country_shapes` is GeoDataFrame with country shapes and iso codes</span>
|
|
<span class="gp">In [7]: </span><span class="n">country_shapes</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
|
|
<span class="gh">Out[7]: </span><span class="go"></span>
|
|
<span class="go"> geometry iso_a3</span>
|
|
<span class="go">0 POLYGON ((61.21081709172574 35.65007233330923,... AFG</span>
|
|
<span class="go">1 (POLYGON ((16.32652835456705 -5.87747039146621... AGO</span>
|
|
<span class="go">2 POLYGON ((20.59024743010491 41.85540416113361,... ALB</span>
|
|
<span class="go">3 POLYGON ((51.57951867046327 24.24549713795111,... ARE</span>
|
|
<span class="go">4 (POLYGON ((-65.50000000000003 -55.199999999999... ARG</span>
|
|
|
|
<span class="go"># `country_names` is DataFrame with country names and iso codes</span>
|
|
<span class="gp">In [8]: </span><span class="n">country_names</span><span class="o">.</span><span class="n">head</span><span class="p">()</span>
|
|
<span class="go"> |