Indexing, Selecting and Exploring spatial data in GeoPandas
Indexing, Selecting and Exploring spatial data in GeoPandas
After reading this tutorial practice, you can have a full basic spatial data analysis in your pocket.
You can do different style of selection of geodataframe in GeoPandas. If you want to select only one column for example:
| 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)| pop_est | continent | name | gdp_md_est | geometry | |
|---|---|---|---|---|---|
| iso_a3 | |||||
| FJI | 920938 | Oceania | Fiji | 8374.0 | MULTIPOLYGON (((180.00000 -16.06713, 180.00000... |
| TZA | 53950935 | Africa | Tanzania | 150600.0 | POLYGON ((33.90371 -0.95000, 34.07262 -1.05982... |
| ESH | 603253 | Africa | W. Sahara | 906.5 | POLYGON ((-8.66559 27.65643, -8.66512 27.58948... |
| CAN | 35623680 | North America | Canada | 1674000.0 | MULTIPOLYGON (((-122.84000 49.00000, -122.9742... |
| USA | 326625791 | North America | United States of America | 18560000.0 | MULTIPOLYGON (((-122.84000 49.00000, -120.0000... |
My country is Senegal and let's extract Senegal and visualized it by using .loc function in geopandas
Analyzing geospatial data
Find gpd of Africa and Europe, then compare them
The second part of this geospatial practice, i will try to find the gdp of African countries and later on compare it to gdp of European Countries. My comparison is not equal from the first observation. We know already the European gdp is much higher than the African gdp, but we are just trying to process, explore and analyze data here.
| 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... |
| 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... |
We need to add a GDP column
Index(['pop_est', 'continent', 'name', 'iso_a3', 'gdp_md_est', 'geometry'], dtype='object')
d:\programm files\python\lib\site-packages\geopandas\geodataframe.py:853: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy super(GeoDataFrame, self).__setitem__(key, value)
Index(['pop_est', 'continent', 'name', 'iso_a3', 'gdp_md_est', 'geometry',
'GDP'],
dtype='object')Index(['pop_est', 'continent', 'name', 'iso_a3', 'gdp_md_est', 'geometry',
'GDP', 'GDP_africa'],
dtype='object')Ooh, we have a GDP per capital for every country in Africa. Note: We use only african countries stated on geopandas datasets, there might be some other states that are dependency.
Our result is a big challenge, because the countries stated here are mostly a little richer than other african countries except Libya for nowadays
Filtering the largest 5 African countries, i'm still looking how to do that
<AxesSubplot:xlabel='name'>
Oooh lovely, it's not my country 'Senegal' the top one, it's Equatorial Guinea, followed by Gabon and Botswana. No one of these countries are in West Africa. Another analyze can be for example, what's make them different from the others. A simple answer can be an oil production for Gabon but that can't be only reason because there are other countries in Africa who produces much more oil than Gabon. I'm here for technical data analysis, so far i will continue on my technical data analysis. To see the figure better, you can download a Magnifying glass from google app extension.
| count | mean | std | min | 25% | 50% | 75% | max | |
|---|---|---|---|---|---|---|---|---|
| name | ||||||||
| Algeria | 1.0 | 0.014875 | NaN | 0.014875 | 0.014875 | 0.014875 | 0.014875 | 0.014875 |
| Angola | 1.0 | 0.006448 | NaN | 0.006448 | 0.006448 | 0.006448 | 0.006448 | 0.006448 |
| Benin | 1.0 | 0.002202 | NaN | 0.002202 | 0.002202 | 0.002202 | 0.002202 | 0.002202 |
| Botswana | 1.0 | 0.016209 | NaN | 0.016209 | 0.016209 | 0.016209 | 0.016209 | 0.016209 |
| Burkina Faso | 1.0 | 0.001641 | NaN | 0.001641 | 0.001641 | 0.001641 | 0.001641 | 0.001641 |
| Burundi | 1.0 | 0.000688 | NaN | 0.000688 | 0.000688 | 0.000688 | 0.000688 | 0.000688 |
| Cameroon | 1.0 | 0.003090 | NaN | 0.003090 | 0.003090 | 0.003090 | 0.003090 | 0.003090 |
| Central African Rep. | 1.0 | 0.000570 | NaN | 0.000570 | 0.000570 | 0.000570 | 0.000570 | 0.000570 |
| Chad | 1.0 | 0.002533 | NaN | 0.002533 | 0.002533 | 0.002533 | 0.002533 | 0.002533 |
| Congo | 1.0 | 0.006109 | NaN | 0.006109 | 0.006109 | 0.006109 | 0.006109 | 0.006109 |
| Côte d'Ivoire | 1.0 | 0.003602 | NaN | 0.003602 | 0.003602 | 0.003602 | 0.003602 | 0.003602 |
| Dem. Rep. Congo | 1.0 | 0.000792 | NaN | 0.000792 | 0.000792 | 0.000792 | 0.000792 | 0.000792 |
| Djibouti | 1.0 | 0.003866 | NaN | 0.003866 | 0.003866 | 0.003866 | 0.003866 | 0.003866 |
| Egypt | 1.0 | 0.011387 | NaN | 0.011387 | 0.011387 | 0.011387 | 0.011387 | 0.011387 |
| Eq. Guinea | 1.0 | 0.040817 | NaN | 0.040817 | 0.040817 | 0.040817 | 0.040817 | 0.040817 |
| Eritrea | 1.0 | 0.001549 | NaN | 0.001549 | 0.001549 | 0.001549 | 0.001549 | 0.001549 |
| Ethiopia | 1.0 | 0.001658 | NaN | 0.001658 | 0.001658 | 0.001658 | 0.001658 | 0.001658 |
| Gabon | 1.0 | 0.020302 | NaN | 0.020302 | 0.020302 | 0.020302 | 0.020302 | 0.020302 |
| Gambia | 1.0 | 0.001651 | NaN | 0.001651 | 0.001651 | 0.001651 | 0.001651 | 0.001651 |
| Ghana | 1.0 | 0.004393 | NaN | 0.004393 | 0.004393 | 0.004393 | 0.004393 | 0.004393 |
| Guinea | 1.0 | 0.001295 | NaN | 0.001295 | 0.001295 | 0.001295 | 0.001295 | 0.001295 |
| Guinea-Bissau | 1.0 | 0.001591 | NaN | 0.001591 | 0.001591 | 0.001591 | 0.001591 | 0.001591 |
| Kenya | 1.0 | 0.003207 | NaN | 0.003207 | 0.003207 | 0.003207 | 0.003207 | 0.003207 |
| Lesotho | 1.0 | 0.003074 | NaN | 0.003074 | 0.003074 | 0.003074 | 0.003074 | 0.003074 |
| Liberia | 1.0 | 0.000828 | NaN | 0.000828 | 0.000828 | 0.000828 | 0.000828 | 0.000828 |
| Libya | 1.0 | 0.013661 | NaN | 0.013661 | 0.013661 | 0.013661 | 0.013661 | 0.013661 |
| Madagascar | 1.0 | 0.001471 | NaN | 0.001471 | 0.001471 | 0.001471 | 0.001471 | 0.001471 |
| Malawi | 1.0 | 0.001104 | NaN | 0.001104 | 0.001104 | 0.001104 | 0.001104 | 0.001104 |
| Mali | 1.0 | 0.002130 | NaN | 0.002130 | 0.002130 | 0.002130 | 0.002130 | 0.002130 |
| Mauritania | 1.0 | 0.004446 | NaN | 0.004446 | 0.004446 | 0.004446 | 0.004446 | 0.004446 |
| Morocco | 1.0 | 0.008321 | NaN | 0.008321 | 0.008321 | 0.008321 | 0.008321 | 0.008321 |
| Mozambique | 1.0 | 0.001317 | NaN | 0.001317 | 0.001317 | 0.001317 | 0.001317 | 0.001317 |
| Namibia | 1.0 | 0.010460 | NaN | 0.010460 | 0.010460 | 0.010460 | 0.010460 | 0.010460 |
| Niger | 1.0 | 0.001047 | NaN | 0.001047 | 0.001047 | 0.001047 | 0.001047 | 0.001047 |
| Nigeria | 1.0 | 0.005713 | NaN | 0.005713 | 0.005713 | 0.005713 | 0.005713 | 0.005713 |
| Rwanda | 1.0 | 0.001846 | NaN | 0.001846 | 0.001846 | 0.001846 | 0.001846 | 0.001846 |
| S. Sudan | 1.0 | 0.001603 | NaN | 0.001603 | 0.001603 | 0.001603 | 0.001603 | 0.001603 |
| Senegal | 1.0 | 0.002708 | NaN | 0.002708 | 0.002708 | 0.002708 | 0.002708 | 0.002708 |
| Sierra Leone | 1.0 | 0.001726 | NaN | 0.001726 | 0.001726 | 0.001726 | 0.001726 | 0.001726 |
| Somalia | 1.0 | 0.000627 | NaN | 0.000627 | 0.000627 | 0.000627 | 0.000627 | 0.000627 |
| Somaliland | 1.0 | 0.003500 | NaN | 0.003500 | 0.003500 | 0.003500 | 0.003500 | 0.003500 |
| South Africa | 1.0 | 0.013477 | NaN | 0.013477 | 0.013477 | 0.013477 | 0.013477 | 0.013477 |
| Sudan | 1.0 | 0.004721 | NaN | 0.004721 | 0.004721 | 0.004721 | 0.004721 | 0.004721 |
| Tanzania | 1.0 | 0.002791 | NaN | 0.002791 | 0.002791 | 0.002791 | 0.002791 | 0.002791 |
| Togo | 1.0 | 0.001458 | NaN | 0.001458 | 0.001458 | 0.001458 | 0.001458 | 0.001458 |
| Tunisia | 1.0 | 0.011470 | NaN | 0.011470 | 0.011470 | 0.011470 | 0.011470 | 0.011470 |
| Uganda | 1.0 | 0.002146 | NaN | 0.002146 | 0.002146 | 0.002146 | 0.002146 | 0.002146 |
| W. Sahara | 1.0 | 0.001503 | NaN | 0.001503 | 0.001503 | 0.001503 | 0.001503 | 0.001503 |
| Zambia | 1.0 | 0.004080 | NaN | 0.004080 | 0.004080 | 0.004080 | 0.004080 | 0.004080 |
| Zimbabwe | 1.0 | 0.002052 | NaN | 0.002052 | 0.002052 | 0.002052 | 0.002052 | 0.002052 |
| eSwatini | 1.0 | 0.007538 | NaN | 0.007538 | 0.007538 | 0.007538 | 0.007538 | 0.007538 |
0.2672931076038051
Let's stop here with our analyzing for Africa, now let's focus on Europe which is the comparison
As talking about Europe here, my heart is going to Scotland, i love you Alba, i remember you Lucy, the lovely child, great child in great place. As my daily routine was waiting my lovely daughter Aysha after dropping her at school, one day, i stopped somewhere and a great man over there came to me and said gently, did you sow your car tire condition? ooh my dear, it was totally broken, thank to him always and God bless you wherever you are.
| pop_est | continent | name | iso_a3 | gdp_md_est | geometry | |
|---|---|---|---|---|---|---|
| 18 | 142257519 | Europe | Russia | RUS | 3745000.0 | MULTIPOLYGON (((178.725 71.099, 180.000 71.516... |
| 21 | 5320045 | Europe | Norway | -99 | 364700.0 | MULTIPOLYGON (((15.143 79.674, 15.523 80.016, ... |
| 43 | 67106161 | Europe | France | -99 | 2699000.0 | MULTIPOLYGON (((-51.658 4.156, -52.249 3.241, ... |
| 110 | 9960487 | Europe | Sweden | SWE | 498100.0 | POLYGON ((11.027 58.856, 11.468 59.432, 12.300... |
| 111 | 9549747 | Europe | Belarus | BLR | 165400.0 | POLYGON ((28.177 56.169, 29.230 55.918, 29.372... |
['Russia', 'Norway', 'France', 'Sweden', 'Belarus', 'Ukraine', 'Poland', 'Austria', 'Hungary', 'Moldova', 'Romania', 'Lithuania', 'Latvia', 'Estonia', 'Germany', 'Bulgaria', 'Greece', 'Albania', 'Croatia', 'Switzerland', 'Luxembourg', 'Belgium', 'Netherlands', 'Portugal', 'Spain', 'Ireland', 'Italy', 'Denmark', 'United Kingdom', 'Iceland', 'Slovenia', 'Finland', 'Slovakia', 'Czechia', 'Bosnia and Herz.', 'Macedonia', 'Serbia', 'Montenegro', 'Kosovo']
d:\programm files\python\lib\site-packages\geopandas\geodataframe.py:853: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy super(GeoDataFrame, self).__setitem__(key, value)
| pop_est | continent | name | iso_a3 | gdp_md_est | geometry | GDP_europe | |
|---|---|---|---|---|---|---|---|
| 18 | 142257519 | Europe | Russia | RUS | 3745000.0 | MULTIPOLYGON (((178.725 71.099, 180.000 71.516... | 0.026325 |
| 21 | 5320045 | Europe | Norway | -99 | 364700.0 | MULTIPOLYGON (((15.143 79.674, 15.523 80.016, ... | 0.068552 |
| 43 | 67106161 | Europe | France | -99 | 2699000.0 | MULTIPOLYGON (((-51.658 4.156, -52.249 3.241, ... | 0.040220 |
| 110 | 9960487 | Europe | Sweden | SWE | 498100.0 | POLYGON ((11.027 58.856, 11.468 59.432, 12.300... | 0.050008 |
| 111 | 9549747 | Europe | Belarus | BLR | 165400.0 | POLYGON ((28.177 56.169, 29.230 55.918, 29.372... | 0.017320 |
| pop_est | continent | name | iso_a3 | gdp_md_est | geometry | GDP_europe | |
|---|---|---|---|---|---|---|---|
| 128 | 594130 | Europe | Luxembourg | LUX | 58740.0 | POLYGON ((6.04307 50.12805, 6.24275 49.90223, ... | 0.098867 |
| 21 | 5320045 | Europe | Norway | -99 | 364700.0 | MULTIPOLYGON (((15.14282 79.67431, 15.52255 80... | 0.068552 |
| 133 | 5011102 | Europe | Ireland | IRL | 322000.0 | POLYGON ((-6.19788 53.86757, -6.03299 53.15316... | 0.064257 |
| 127 | 8236303 | Europe | Switzerland | CHE | 496300.0 | POLYGON ((9.59423 47.52506, 9.63293 47.34760, ... | 0.060258 |
| 130 | 17084719 | Europe | Netherlands | NLD | 870800.0 | POLYGON ((6.90514 53.48216, 7.09205 53.14404, ... | 0.050970 |
<AxesSubplot:>
<AxesSubplot:xlabel='name'>
The highest gdp in Europe in Luxembourg and lowest is Kosovo, but both country are having a little similarity about area
1.314297770189312
The total gdp of europe is higher than africa with asimple eye observation. Let's calculate the difference
1.0470046625855067
After analyzing European GDP, let's see the world GDP
{'Africa',
'Antarctica',
'Asia',
'Europe',
'North America',
'Oceania',
'Seven seas (open ocean)',
'South America'}False
<AxesSubplot:>
<AxesSubplot:>
The last two figures are different, you can see it by yourself
Just a small notification for readers: White analyzing any data, there are thousands of ideas coming around and sometimes you need to show up your experiences and skills
d:\programm files\python\lib\site-packages\geopandas\geodataframe.py:853: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy super(GeoDataFrame, self).__setitem__(key, value)
| pop_est | continent | name | iso_a3 | gdp_md_est | geometry | gdp_per_cap | |
|---|---|---|---|---|---|---|---|
| 0 | 920938 | Oceania | Fiji | FJI | 8374.0 | MULTIPOLYGON (((180.00000 -16.06713, 180.00000... | 0.009093 |
| 1 | 53950935 | Africa | Tanzania | TZA | 150600.0 | POLYGON ((33.90371 -0.95000, 34.07262 -1.05982... | 0.002791 |
| 2 | 603253 | Africa | W. Sahara | ESH | 906.5 | POLYGON ((-8.66559 27.65643, -8.66512 27.58948... | 0.001503 |
| 3 | 35623680 | North America | Canada | CAN | 1674000.0 | MULTIPOLYGON (((-122.84000 49.00000, -122.9742... | 0.046991 |
| 4 | 326625791 | North America | United States of America | USA | 18560000.0 | MULTIPOLYGON (((-122.84000 49.00000, -120.0000... | 0.056823 |
<AxesSubplot:>
Exploring the World Population density with color
<AxesSubplot:>
China seems to be the most popular
<AxesSubplot:>
Nigeria is the most popular country in Africa
<AxesSubplot:>
Russia seems to be the most populest country in Europe
<AxesSubplot:>
<AxesSubplot:>
<AxesSubplot:>
The most south part of the globe is less with gdp except Australia and Golf countries
d:\programm files\python\lib\site-packages\geopandas\geodataframe.py:853: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy super(GeoDataFrame, self).__setitem__(key, value)
Index(['pop_est', 'continent', 'name', 'iso_a3', 'gdp_md_est', 'geometry',
'gdp_per_cap'],
dtype='object')<AxesSubplot:>
Here, we can see clearly that the northern part of Africa and Southern Part are much richer than the rest.
<AxesSubplot:>
<AxesSubplot:>
<AxesSubplot:>
Exploring missing data in Geopandas
Exploring spatial data is just fantastic, however, when we have a spatial data that contains a missing values. Geopandas from the version 0.7 to higher version ignores it automatically.
We will create a data from world['pop_est'], 40 rows and transform them to nan or NAN, sometimes NaN. We used .loc to concentrate in one specific column that will not affect other columns values.
| pop_est | continent | name | iso_a3 | gdp_md_est | geometry | gdp_per_cap | |
|---|---|---|---|---|---|---|---|
| 0 | NaN | Oceania | Fiji | FJI | 8374.0 | MULTIPOLYGON (((180.00000 -16.06713, 180.00000... | 0.009093 |
| 1 | 53950935.0 | Africa | Tanzania | TZA | 150600.0 | POLYGON ((33.90371 -0.95000, 34.07262 -1.05982... | 0.002791 |
| 2 | 603253.0 | Africa | W. Sahara | ESH | 906.5 | POLYGON ((-8.66559 27.65643, -8.66512 27.58948... | 0.001503 |
| 3 | 35623680.0 | North America | Canada | CAN | 1674000.0 | MULTIPOLYGON (((-122.84000 49.00000, -122.9742... | 0.046991 |
| 4 | NaN | North America | United States of America | USA | 18560000.0 | MULTIPOLYGON (((-122.84000 49.00000, -120.0000... | 0.056823 |
| pop_est | continent | name | iso_a3 | gdp_md_est | geometry | gdp_per_cap | |
|---|---|---|---|---|---|---|---|
| 0 | NaN | Oceania | Fiji | FJI | 8374.0 | MULTIPOLYGON (((180.00000 -16.06713, 180.00000... | 0.009093 |
| 4 | NaN | North America | United States of America | USA | 18560000.0 | MULTIPOLYGON (((-122.84000 49.00000, -120.0000... | 0.056823 |
| 7 | NaN | Oceania | Papua New Guinea | PNG | 28020.0 | MULTIPOLYGON (((141.00021 -2.60015, 142.73525 ... | 0.004055 |
| 16 | NaN | North America | Haiti | HTI | 19340.0 | POLYGON ((-71.71236 19.71446, -71.62487 19.169... | 0.001817 |
| 19 | NaN | North America | Bahamas | BHS | 9066.0 | MULTIPOLYGON (((-78.98000 26.79000, -78.51000 ... | 0.027474 |
| ... | ... | ... | ... | ... | ... | ... | ... |
| 161 | NaN | Asia | Cyprus | CYP | 29260.0 | POLYGON ((32.73178 35.14003, 32.91957 35.08783... | 0.023953 |
| 167 | NaN | Africa | Somaliland | -99 | 12250.0 | POLYGON ((48.94820 11.41062, 48.94820 11.41062... | 0.003500 |
| 169 | NaN | Africa | Rwanda | RWA | 21970.0 | POLYGON ((30.41910 -1.13466, 30.81613 -1.69891... | 0.001846 |
| 170 | NaN | Europe | Bosnia and Herz. | BIH | 42530.0 | POLYGON ((18.56000 42.65000, 17.67492 43.02856... | 0.011029 |
| 173 | NaN | Europe | Montenegro | MNE | 10610.0 | POLYGON ((20.07070 42.58863, 19.80161 42.50009... | 0.016512 |
66 rows × 7 columns
<AxesSubplot:>
<AxesSubplot:>
Mapping with Layers
I might have a different geometry points locations and want to map them in specific map. To be clearer, i have bike sations addresses in Paris and want to map them (display) on Paris map or France map. The Point locations are represented with layers. It's very important to make sure that the data are always having similar crs (coordinate reference system).
| 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) |
<AxesSubplot:>
pyproj.crs.crs.CRS
We make sure that we have full world map first then check if the cities crs is same with world crs.
| 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... |
Note: Our globe data is not having gdp_per_cap column
Index(['pop_est', 'continent', 'name', 'iso_a3', 'gdp_md_est', 'geometry'], dtype='object')
pyproj.crs.crs.CRS
<Geographic 2D CRS: EPSG:4326> Name: WGS 84 Axis Info [ellipsoidal]: - Lat[north]: Geodetic latitude (degree) - Lon[east]: Geodetic longitude (degree) Area of Use: - name: World - bounds: (-180.0, -90.0, 180.0, 90.0) Datum: World Geodetic System 1984 - Ellipsoid: WGS 84 - Prime Meridian: Greenwich
<Geographic 2D CRS: EPSG:4326> Name: WGS 84 Axis Info [ellipsoidal]: - Lat[north]: Geodetic latitude (degree) - Lon[east]: Geodetic longitude (degree) Area of Use: - name: World - bounds: (-180.0, -90.0, 180.0, 90.0) Datum: World Geodetic System 1984 - Ellipsoid: WGS 84 - Prime Meridian: Greenwich
I think they have same crs.
<AxesSubplot:>
I will experiment the last code with Senegal and see how it will look like
| 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... |
<AxesSubplot:>
I will upload some region in Senegal and try to map them
| city | Region | Long | Lat | |
|---|---|---|---|---|
| 0 | Bigno | Dakar | -16.22639 | 12.81028 |
| 1 | Dakar | Dakar | -17.44406 | 14.69370 |
| 2 | Daara | Louga | -15.47993 | 15.34844 |
| 3 | Diawara | Matam | -12.54374 | 15.02196 |
| 4 | Diofior | Fatick | -16.66667 | 14.18333 |
As i don't have geometry column ('its only a pandas dataframe not geodataframe since the columns does not include geometry column'), i will create it. To do that, this is an idea: polygon_geom = zip(lon_list, lat_list)
array(['city', 'Region', 'Long', 'Lat', 'geometry'], dtype=object)
| city | Region | Long | Lat | geometry | |
|---|---|---|---|---|---|
| 0 | Bigno | Dakar | -16.22639 | 12.81028 | POINT (-16.22639 12.81028) |
| 1 | Dakar | Dakar | -17.44406 | 14.69370 | POINT (-17.44406 14.6937) |
| 2 | Daara | Louga | -15.47993 | 15.34844 | POINT (-15.47993 15.34844) |
| 3 | Diawara | Matam | -12.54374 | 15.02196 | POINT (-12.54374 15.02196) |
| 4 | Diofior | Fatick | -16.66667 | 14.18333 | POINT (-16.66667 14.18333) |
After creating crs for our excel loaded data, we still don't have a geodataframe data, for example if we plot it, we get something different.
<AxesSubplot:>
| city | Region | Long | Lat | geometry | |
|---|---|---|---|---|---|
| 0 | Bigno | Dakar | -16.22639 | 12.81028 | POINT (-16.22639 12.81028) |
| 1 | Dakar | Dakar | -17.44406 | 14.69370 | POINT (-17.44406 14.69370) |
| 2 | Daara | Louga | -15.47993 | 15.34844 | POINT (-15.47993 15.34844) |
| 3 | Diawara | Matam | -12.54374 | 15.02196 | POINT (-12.54374 15.02196) |
| 4 | Diofior | Fatick | -16.66667 | 14.18333 | POINT (-16.66667 14.18333) |
geopandas.geodataframe.GeoDataFrame
<AxesSubplot:>
Oooh that's it, let's map them in Senegal map.
<AxesSubplot:>
<Geographic 2D CRS: EPSG:4326> Name: WGS 84 Axis Info [ellipsoidal]: - Lat[north]: Geodetic latitude (degree) - Lon[east]: Geodetic longitude (degree) Area of Use: - name: World - bounds: (-180.0, -90.0, 180.0, 90.0) Datum: World Geodetic System 1984 - Ellipsoid: WGS 84 - Prime Meridian: Greenwich
None
| city | Region | Long | Lat | geometry | |
|---|---|---|---|---|---|
| 0 | Bigno | Dakar | -16.22639 | 12.81028 | POINT (-16.22639 12.81028) |
| 1 | Dakar | Dakar | -17.44406 | 14.69370 | POINT (-17.44406 14.69370) |
| 2 | Daara | Louga | -15.47993 | 15.34844 | POINT (-15.47993 15.34844) |
| 3 | Diawara | Matam | -12.54374 | 15.02196 | POINT (-12.54374 15.02196) |
| 4 | Diofior | Fatick | -16.66667 | 14.18333 | POINT (-16.66667 14.18333) |
| 5 | Diourbel | Diourbel | -16.24363 | 14.64792 | POINT (-16.24363 14.64792) |
| 6 | Fatick | Fatick | -16.58583 | 14.35806 | POINT (-16.58583 14.35806) |
| 7 | Foundioungne | Fatick | -16.46667 | 14.13333 | POINT (-16.46667 14.13333) |
| 8 | Gandiaye | Kaolack | -16.26667 | 14.23333 | POINT (-16.26667 14.23333) |
| 9 | Goléré | Saint-Louis | -14.10165 | 16.25575 | POINT (-14.10165 16.25575) |
<class 'geopandas.geodataframe.GeoDataFrame'> RangeIndex: 10 entries, 0 to 9 Data columns (total 5 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 city 10 non-null object 1 Region 10 non-null object 2 Long 10 non-null float64 3 Lat 10 non-null float64 4 geometry 10 non-null geometry dtypes: float64(2), geometry(1), object(2) memory usage: 344.0+ bytes
<AxesSubplot:>
Just to notify that party in Senegal map look like an open river or mouth, it's Gambia
Again, another method to display the main capital city of every country in world (mapping layers)
<AxesSubplot:>
<AxesSubplot:>
This method of displaying layers is based on geometric polygon order in the datasets, to change the order, we need to use 'zorder' attribute
Conclusion:
At the end of this practical tutorial, we could explore geopandas datasets, analyzing spatial data and interact with a specific area in the world, explore GDP for any country and many more.
While writing this tutorial, i remember you Lucy from Alba, my great love to you a shiny child.
Comments
Post a Comment