{"id":969,"date":"2022-09-16T14:27:57","date_gmt":"2022-09-16T14:27:57","guid":{"rendered":"https:\/\/ml-gis-service.com\/?p=969"},"modified":"2022-09-16T14:27:57","modified_gmt":"2022-09-16T14:27:57","slug":"toolbox-pandas-dataframe-into-geopandas-geodataframe","status":"publish","type":"post","link":"https:\/\/ml-gis-service.com\/index.php\/2022\/09\/16\/toolbox-pandas-dataframe-into-geopandas-geodataframe\/","title":{"rendered":"Toolbox: Pandas DataFrame into GeoPandas GeoDataFrame"},"content":{"rendered":"\n<p>Have you ever had problems visualizing spatial data read from a <code>csv<\/code> file? Is your spatial data stored in flat tables with <code>lon<\/code> \/ <code>lat<\/code> columns? I encountered those problems, that&#8217;s why I use a simple to transform quickly transform <code>DataFrame<\/code> into <code>GeoDataFrame<\/code>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import geopandas as gpd\n\n\ndef df2gdf(df, lon_col='x', lat_col='y', epsg=4326, crs=None):\n    \"\"\"\n    Function transforms DataFrame into GeoDataFrame.\n    \n    Parameters\n    ----------\n    df : pandas DataFrame\n    \n    lon_col : str, default = 'x'\n        Longitude column name.\n        \n    lat_col : str, default = 'y'\n        Latitude column name.\n        \n    epsg : Union[int, str], default = 4326\n        EPSG number of projection.\n        \n    crs : str, default = None\n        Coordinate Reference System of data.\n        \n    Returns\n    -------\n    gdf : GeoPandas GeoDataFrame\n        GeoDataFrame with set geometry column ('geometry'), CRS, and all columns from the passed DataFrame.\n    \n    \"\"\"\n    \n    gdf = gpd.GeoDataFrame(df)\n    gdf['geometry'] = gpd.points_from_xy(x=df[lon_col], y=df[lat_col])\n    gdf.geometry = gdf['geometry']\n    \n    if crs is None:\n        gdf.set_crs(epsg=epsg, inplace=True)\n    else:\n        gdf.set_crs(crs=crs, inplace=True)\n        \n    return gdf\n\n<\/pre>\n\n\n\n<p>The basic differences between <code>DataFrame<\/code> and <code>GeoDataFrame<\/code> are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>column with the <code>geometry<\/code> data types,<\/li><li>projection (coordinate reference system).<\/li><\/ul>\n\n\n\n<p>The function <code>df2gdf()<\/code> takes <code>DataFrame<\/code>, longitude column, latitude column, and <code>crs<\/code> or <code>epsg<\/code> to set a valid projection of the output structure. If you have provided <code>DataFrame<\/code> with a column that stores geometries (Point, Line, Polygon) then, the line <code>gdf['geometry'] = gpd.points_from_xy(x=df[lon_col], y=df[lat_col])<\/code> is not required here. However, I assume that when we read a plain table, then geometries are provided as <code>x<\/code> and <code>y<\/code> columns, or <code>lon<\/code> and <code>lat<\/code> columns. In this scenario, we must transform a tuple of <code>float<\/code>s.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Transform DataFrame to GeoDataFrame in Python.<\/p>\n","protected":false},"author":1,"featured_media":971,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79,3,17],"tags":[85,228,227,57,64,7],"class_list":["post-969","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pandas","category-python","category-scripts","tag-dataframe","tag-dataframe-to-geodataframe","tag-geodataframe","tag-geopandas","tag-pandas","tag-python"],"_links":{"self":[{"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/posts\/969","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/comments?post=969"}],"version-history":[{"count":2,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/posts\/969\/revisions"}],"predecessor-version":[{"id":972,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/posts\/969\/revisions\/972"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/media\/971"}],"wp:attachment":[{"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/media?parent=969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/categories?post=969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ml-gis-service.com\/index.php\/wp-json\/wp\/v2\/tags?post=969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}