Extracting a specific city in Geopandas Dataset
Extracting a specific city in Geopandas Dataset
In the following lines, i will try to show you how to extract your own city from geopandas dataset without uploading any other data.
| pop_est | continent | name | iso_a3 | gdp_md_est | geometry | |
|---|---|---|---|---|---|---|
| 0 | 920938 | Oceania | Fiji | FJI | 8374.0 | MULTIPOLYGON (((180.00000 -16.06713, 180.00000... |
| 1 | 53950935 | Africa | Tanzania | TZA | 150600.0 | POLYGON ((33.90371 -0.95000, 34.07262 -1.05982... |
| 2 | 603253 | Africa | W. Sahara | ESH | 906.5 | POLYGON ((-8.66559 27.65643, -8.66512 27.58948... |
| 3 | 35623680 | North America | Canada | CAN | 1674000.0 | MULTIPOLYGON (((-122.84000 49.00000, -122.9742... |
| 4 | 326625791 | North America | United States of America | USA | 18560000.0 | MULTIPOLYGON (((-122.84000 49.00000, -120.0000... |
array(['pop_est', 'continent', 'name', 'iso_a3', 'gdp_md_est', 'geometry'],
dtype=object)<class 'geopandas.geodataframe.GeoDataFrame'> RangeIndex: 177 entries, 0 to 176 Data columns (total 6 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 pop_est 177 non-null int64 1 continent 177 non-null object 2 name 177 non-null object 3 iso_a3 177 non-null object 4 gdp_md_est 177 non-null float64 5 geometry 177 non-null geometry dtypes: float64(1), geometry(1), int64(1), object(3) memory usage: 5.6+ KB
<AxesSubplot:>
We have global world map here, let's for example extracte only african contient from the global map
| pop_est | continent | name | iso_a3 | gdp_md_est | geometry | |
|---|---|---|---|---|---|---|
| 1 | 53950935 | Africa | Tanzania | TZA | 150600.0 | POLYGON ((33.90371 -0.95000, 34.07262 -1.05982... |
| 2 | 603253 | Africa | W. Sahara | ESH | 906.5 | POLYGON ((-8.66559 27.65643, -8.66512 27.58948... |
| 11 | 83301151 | Africa | Dem. Rep. Congo | COD | 66010.0 | POLYGON ((29.34000 -4.49998, 29.51999 -5.41998... |
| 12 | 7531386 | Africa | Somalia | SOM | 4719.0 | POLYGON ((41.58513 -1.68325, 40.99300 -0.85829... |
| 13 | 47615739 | Africa | Kenya | KEN | 152700.0 | POLYGON ((39.20222 -4.67677, 37.76690 -3.67712... |
<AxesSubplot:>
I'm from Senegal and i want to extract my country, let's try it
| pop_est | continent | name | iso_a3 | gdp_md_est | geometry | |
|---|---|---|---|---|---|---|
| 51 | 14668522 | Africa | Senegal | SEN | 39720.0 | POLYGON ((-16.71373 13.59496, -17.12611 14.373... |
We can see a 'POLYGON' in the 'geometry', because cities can be Point, countries are Polygons etc...
<AxesSubplot:>
That the Senegal map i've know before and now. But Senegal has different cities and locations. I will see if i can show you the location of Dakar (the capital city of Senegal) in the Senegal map
In Geopandas dataset, there is a dataset named 'naturalearth_cities'. From there, i will try to extract 'Dakar'.
| name | geometry | |
|---|---|---|
| 0 | Vatican City | POINT (12.45339 41.90328) |
| 1 | San Marino | POINT (12.44177 43.93610) |
| 2 | Vaduz | POINT (9.51667 47.13372) |
| 3 | Luxembourg | POINT (6.13000 49.61166) |
| 4 | Palikir | POINT (158.14997 6.91664) |
array(['name', 'geometry'], dtype=object)
<class 'geopandas.geodataframe.GeoDataFrame'> RangeIndex: 202 entries, 0 to 201 Data columns (total 2 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 name 202 non-null object 1 geometry 202 non-null geometry dtypes: geometry(1), object(1) memory usage: 1.6+ KB
<AxesSubplot:>
We have the cities in every country sated in geopandas data. Note: There might be some other countries who's not having specific status and might not be included in this dataset
So far, our figure is not showing mutch information, its only showing us scattered points. Our goal here is to locate only 'Dakar' on 'Senegal' map. To do that, we need to extracte Dakar from cities
| name | geometry | |
|---|---|---|
| 170 | Dakar | POINT (-17.47508 14.71778) |
The Point here describes a city as we explained above. Where is Dakar in Senegal?
<AxesSubplot:>
The red point is indicating the location of Dakar in Senegal map. My next tutorial will try to show all the main cities of Senegal in map
Conclusion: Geopandas is a great library that can help to do a lot of things. We could do all our work here, without uploading any data, that's also great. In fact, there are a lot of ideas behind the geopandas dataset, we can extract for example all african cities in world map or only AFRICA
Comments
Post a Comment