Integrating with GeoPandas¶
As of version 0.4.0, VerticaPy features GeoPandas integration. This allows you to easily export a vDataFrame as a GeoPandas DataFrame, giving you more control over geospatial data.
This example demonstrates the advantages of GeoPandas integration with the world dataset.
import verticapy as vp
from verticapy.datasets import load_world
# Setting the plotting lib
vp.set_option("plotting_lib", "matplotlib")
world = load_world()
world.head(100)
123 pop_est100% | ... | Abc country100% | 🌎 100% | |
| 1 | 57713 | ... | Greenland | |
| 2 | 279070 | ... | New Caledonia | |
| 3 | 339747 | ... | Iceland | |
| 4 | 360346 | ... | Belize | |
| 5 | 1291358 | ... | Timor-Leste | |
| 6 | 1467152 | ... | eSwatini | |
| 7 | 1772255 | ... | Gabon | |
| 8 | 1792338 | ... | Guinea-Bissau | |
| 9 | 1895250 | ... | Kosovo | |
| 10 | 1958042 | ... | Lesotho | |
| 11 | 2103721 | ... | Macedonia | |
| 12 | 2214858 | ... | Botswana | |
| 13 | 2484780 | ... | Namibia | |
| 14 | 2875422 | ... | Kuwait | |
| 15 | 3045191 | ... | Armenia | |
| 16 | 3351827 | ... | Puerto Rico | |
| 17 | 3474121 | ... | Moldova | |
| 18 | 3500000 | ... | Somaliland | |
| 19 | 3753142 | ... | Panama | |
| 20 | 3758571 | ... | Mauritania | |
| 21 | 4510327 | ... | New Zealand | |
| 22 | 4954674 | ... | Congo | |
| 23 | 5445829 | ... | Slovakia | |
| 24 | 6653210 | ... | Libya | |
| 25 | 8754413 | ... | Austria | |
| 26 | 9850845 | ... | Hungary | |
| 27 | 9960487 | ... | Sweden | |
| 28 | 11138234 | ... | Bolivia | |
| 29 | 11147407 | ... | Cuba | |
| 30 | 15460732 | ... | Guatemala | |
| 31 | 15972000 | ... | Zambia | |
| 32 | 17885245 | ... | Mali | |
| 33 | 18556698 | ... | Kazakhstan | |
| 34 | 23508428 | ... | Taiwan | |
| 35 | 24184810 | ... | Côte d'Ivoire | |
| 36 | 25054161 | ... | Madagascar | |
| 37 | 25248140 | ... | North Korea | |
| 38 | 29310273 | ... | Angola | |
| 39 | 29384297 | ... | Nepal | |
| 40 | 29748859 | ... | Uzbekistan | |
| 41 | 265100 | ... | N. Cyprus | |
| 42 | 603253 | ... | W. Sahara | |
| 43 | 758288 | ... | Bhutan | |
| 44 | 920938 | ... | Fiji | |
| 45 | 1221549 | ... | Cyprus | |
| 46 | 3360148 | ... | Uruguay | |
| 47 | 4543126 | ... | Palestine | |
| 48 | 4689021 | ... | Liberia | |
| 49 | 4930258 | ... | Costa Rica | |
| 50 | 5011102 | ... | Ireland | |
| 51 | 5625118 | ... | Central African Rep. | |
| 52 | 5918919 | ... | Eritrea | |
| 53 | 6025951 | ... | Nicaragua | |
| 54 | 6072475 | ... | United Arab Emirates | |
| 55 | 8468555 | ... | Tajikistan | |
| 56 | 9038741 | ... | Honduras | |
| 57 | 10734247 | ... | Dominican Rep. | |
| 58 | 10768477 | ... | Greece | |
| 59 | 11901484 | ... | Rwanda | |
| 60 | 13805084 | ... | Zimbabwe | |
| 61 | 17789267 | ... | Chile | |
| 62 | 19196246 | ... | Malawi | |
| 63 | 21529967 | ... | Romania | |
| 64 | 28571770 | ... | Saudi Arabia | |
| 65 | 31304016 | ... | Venezuela | |
| 66 | 39192111 | ... | Iraq | |
| 67 | 39570125 | ... | Uganda | |
| 68 | 40969443 | ... | Algeria | |
| 69 | 54841552 | ... | South Africa | |
| 70 | 80594017 | ... | Germany | |
| 71 | 82021564 | ... | Iran | |
| 72 | 83301151 | ... | Dem. Rep. Congo | |
| 73 | 96160163 | ... | Vietnam | |
| 74 | 105350020 | ... | Ethiopia | |
| 75 | 207353391 | ... | Brazil | |
| 76 | 1281935911 | ... | India | |
| 77 | 282814 | ... | Vanuatu | |
| 78 | 329988 | ... | Bahamas | |
| 79 | 443593 | ... | Brunei | |
| 80 | 594130 | ... | Luxembourg | |
| 81 | 737718 | ... | Guyana | |
| 82 | 865267 | ... | Djibouti | |
| 83 | 1218208 | ... | Trinidad and Tobago | |
| 84 | 1251581 | ... | Estonia | |
| 85 | 1972126 | ... | Slovenia | |
| 86 | 2051363 | ... | Gambia | |
| 87 | 2823859 | ... | Lithuania | |
| 88 | 2990561 | ... | Jamaica | |
| 89 | 3068243 | ... | Mongolia | |
| 90 | 3856181 | ... | Bosnia and Herz. | |
| 91 | 4292095 | ... | Croatia | |
| 92 | 5789122 | ... | Kyrgyzstan | |
| 93 | 6163195 | ... | Sierra Leone | |
| 94 | 6172011 | ... | El Salvador | |
| 95 | 7111024 | ... | Serbia | |
| 96 | 7126706 | ... | Laos | |
| 97 | 7531386 | ... | Somalia | |
| 98 | 7965055 | ... | Togo | |
| 99 | 8236303 | ... | Switzerland | |
| 100 | 8299706 | ... | Israel |
The apply() function of the VerticaPy stats module allows you to apply any Vertica function to the data. Let’s compute the area of each country.
import verticapy.sql.functions as fun
world["geography"] = fun.apply("stv_geography", world["geometry"])
world["geography"].astype("geography")
world["area"] = fun.apply("st_area", world["geography"])
world.head(100)
123 pop_est100% | ... | 🌎 100% | 123 area100% | |
| 1 | 57713 | ... | 2189751414857.83 | |
| 2 | 279070 | ... | 23282206866.0818 | |
| 3 | 339747 | ... | 107031853684.148 | |
| 4 | 360346 | ... | 22118506248.1485 | |
| 5 | 1291358 | ... | 14776389256.2834 | |
| 6 | 1467152 | ... | 18151531542.9276 | |
| 7 | 1772255 | ... | 270683290581.716 | |
| 8 | 1792338 | ... | 36333129902.2658 | |
| 9 | 1895250 | ... | 11211660926.8689 | |
| 10 | 1958042 | ... | 27538833464.2252 | |
| 11 | 2103721 | ... | 25026416110.0201 | |
| 12 | 2214858 | ... | 593439013734.129 | |
| 13 | 2484780 | ... | 827008137847.344 | |
| 14 | 2875422 | ... | 16673251066.4255 | |
| 15 | 3045191 | ... | 28624801795.8552 | |
| 16 | 3351827 | ... | 9253937792.64593 | |
| 17 | 3474121 | ... | 32231982684.4869 | |
| 18 | 3500000 | ... | 168036707875.77 | |
| 19 | 3753142 | ... | 75581062586.3847 | |
| 20 | 3758571 | ... | 1057124493617.98 | |
| 21 | 4510327 | ... | 277235027306.03 | |
| 22 | 4954674 | ... | 341198516478.001 | |
| 23 | 5445829 | ... | 46922595108.0112 | |
| 24 | 6653210 | ... | 1636524223070.76 | |
| 25 | 8754413 | ... | 84824085671.1678 | |
| 26 | 9850845 | ... | 92223563486.7973 | |
| 27 | 9960487 | ... | 447872323030.887 | |
| 28 | 11138234 | ... | 1088907530803.22 | |
| 29 | 11147407 | ... | 115172058712.546 | |
| 30 | 15460732 | ... | 109842365175.189 | |
| 31 | 15972000 | ... | 754741572356.983 | |
| 32 | 17885245 | ... | 1239601857911.99 | |
| 33 | 18556698 | ... | 2721832544138.42 | |
| 34 | 23508428 | ... | 34425102017.393 | |
| 35 | 24184810 | ... | 331227597302.727 | |
| 36 | 25054161 | ... | 591144813591.594 | |
| 37 | 25248140 | ... | 125542308783.322 | |
| 38 | 29310273 | ... | 1250257451714.37 | |
| 39 | 29384297 | ... | 150930213526.882 | |
| 40 | 29748859 | ... | 460739190418.327 | |
| 41 | 265100 | ... | 3786381407.62268 | |
| 42 | 603253 | ... | 96485945529.7125 | |
| 43 | 758288 | ... | 39441951914.4718 | |
| 44 | 920938 | ... | 19353579665.1023 | |
| 45 | 1221549 | ... | 6207615256.1115 | |
| 46 | 3360148 | ... | 176951396583.201 | |
| 47 | 4543126 | ... | 5040777637.52613 | |
| 48 | 4689021 | ... | 98630883299.1132 | |
| 49 | 4930258 | ... | 54052091511.1307 | |
| 50 | 5011102 | ... | 58217540791.3509 | |
| 51 | 5625118 | ... | 624537721851.14 | |
| 52 | 5918919 | ... | 119741273687.089 | |
| 53 | 6025951 | ... | 130035380109.286 | |
| 54 | 6072475 | ... | 80064396581.0864 | |
| 55 | 8468555 | ... | 138010387019.761 | |
| 56 | 9038741 | ... | 114200451680.527 | |
| 57 | 10734247 | ... | 48306262335.7236 | |
| 58 | 10768477 | ... | 131854192993.303 | |
| 59 | 11901484 | ... | 23469928090.8351 | |
| 60 | 13805084 | ... | 377484760106.479 | |
| 61 | 17789267 | ... | 814371484921.802 | |
| 62 | 19196246 | ... | 111617431346.822 | |
| 63 | 21529967 | ... | 237780079437.119 | |
| 64 | 28571770 | ... | 1924600282272.59 | |
| 65 | 31304016 | ... | 912366830926.293 | |
| 66 | 39192111 | ... | 437184378903.089 | |
| 67 | 39570125 | ... | 246868436060.57 | |
| 68 | 40969443 | ... | 2319361725724.86 | |
| 69 | 54841552 | ... | 1218030001843.07 | |
| 70 | 80594017 | ... | 356134219989.012 | |
| 71 | 82021564 | ... | 1618552483050.3 | |
| 72 | 83301151 | ... | 2333671350442.98 | |
| 73 | 96160163 | ... | 337108613977.317 | |
| 74 | 105350020 | ... | 1137102568658.21 | |
| 75 | 207353391 | ... | 8540950345692.85 | |
| 76 | 1281935911 | ... | 3150427056043.35 | |
| 77 | 282814 | ... | 7516451865.1763 | |
| 78 | 329988 | ... | 15615897741.1302 | |
| 79 | 443593 | ... | 10747411137.2812 | |
| 80 | 594130 | ... | 2408816376.51917 | |
| 81 | 737718 | ... | 210721444770.898 | |
| 82 | 865267 | ... | 21966242368.5798 | |
| 83 | 1218208 | ... | 7769152795.90834 | |
| 84 | 1251581 | ... | 44441790595.9614 | |
| 85 | 1972126 | ... | 19070599769.6852 | |
| 86 | 2051363 | ... | 14084033590.0507 | |
| 87 | 2823859 | ... | 63539126862.9918 | |
| 88 | 2990561 | ... | 12500311840.4183 | |
| 89 | 3068243 | ... | 1540264832849.55 | |
| 90 | 3856181 | ... | 50502366239.6244 | |
| 91 | 4292095 | ... | 57402973305.489 | |
| 92 | 5789122 | ... | 195599523413.606 | |
| 93 | 6163195 | ... | 76298870280.0899 | |
| 94 | 6172011 | ... | 20971345487.6227 | |
| 95 | 7111024 | ... | 76232737912.4884 | |
| 96 | 7126706 | ... | 229798138060.241 | |
| 97 | 7531386 | ... | 486443125709.23 | |
| 98 | 7965055 | ... | 61222637781.2825 | |
| 99 | 8236303 | ... | 46063257324.0393 | |
| 100 | 8299706 | ... | 23010251034.8654 |
We can now export our vDataFrame as a GeoPandas DataFrame.
df = world.to_geopandas(geometry = "geometry")
df.head(200)
Out[2]:
pop_est continent country geography area geometry
0 57713 North America Greenland b'\x96\xaa\xa0\x00\x00\x00\x00\x00\x00\x00\x00... 2.189751e+12 POLYGON ((-46.76379 82.62796, -43.40644 83.225...
1 279070 Oceania New Caledonia b'\xa9U^\x18\x00\x00\x00\x00\x00\x00\x00\x00\x... 2.328221e+10 POLYGON ((165.77999 -21.08, 166.59999 -21.7000...
2 339747 Europe Iceland b'\x96\xaa\xac\x00\x00\x00\x00\x00\x00\x00\x00... 1.070319e+11 POLYGON ((-14.5087 66.45589, -14.73964 65.8087...
3 360346 North America Belize b'\x96\xaa\xa3\x00\x00\x00\x00\x00\x00\x00\x00... 2.211851e+10 POLYGON ((-89.14308 17.80832, -89.15091 17.955...
4 1291358 Asia Timor-Leste b'\xa9U\\\xfc\x00\x00\x00\x00\x00\x00\x00\x00\... 1.477639e+10 POLYGON ((124.96868 -8.89279, 125.08625 -8.656...
.. ... ... ... ... ... ...
172 68414135 Asia Thailand b'\xbc\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00... 5.119377e+11 POLYGON ((105.21878 14.27321, 104.28142 14.416...
173 104256076 Asia Philippines b'\xbc\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00... 2.933923e+11 MULTIPOLYGON (((120.8339 12.7045, 120.32344 13...
174 124574795 North America Mexico b'\x96\xaa\xa0\x00\x00\x00\x00\x00\x00\x00\x00... 1.973907e+12 POLYGON ((-117.12776 32.53534, -115.99135 32.6...
175 326625791 North America United States of America b'\x96\xaa\xa0\x00\x00\x00\x00\x00\x00\x00\x00... 9.494301e+12 MULTIPOLYGON (((-122.84 49, -120 49, -117.0312...
176 1379302771 Asia China b'\xbc\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00... 9.408026e+12 MULTIPOLYGON (((109.47521 18.1977, 108.65521 1...
[177 rows x 6 columns]
From there, we can draw any geospatial object.
ax = df.plot(
edgecolor = "black",
color = "white",
figsize = (10, 9),
)
ax.set_title("World Map")
Out[4]: Text(0.5, 1.0, 'World Map')
from verticapy.datasets import load_cities
# Loading the cities dataset
cities = load_cities()
import matplotlib.pyplot as plt
# Creating a Matplotlib figure
fig, ax = plt.subplots()
fig.set_size_inches(11, 8)
# Extracting longitude and latitude
cities["lon"] = fun.apply("st_x", cities["geometry"])
cities["lat"] = fun.apply("st_y", cities["geometry"])
# Drawing the data on a Map
ax = cities.scatter(["lon", "lat"], ax = ax)
df.plot(
edgecolor = "black",
color = "white",
ax = ax,
)
Out[13]: <Axes: xlabel='lon', ylabel='lat'>
You can also draw maps using the geo_plot() method.
from verticapy.datasets import load_africa_education
# Africa Dataset
africa = load_africa_education()
africa_world = load_world()
africa_world = africa_world[africa_world["continent"] == "Africa"]
ax = africa_world["geometry"].geo_plot(
color = "white",
edgecolor = 'black',
)
# displaying schools in Africa
africa.scatter(
["lon", "lat"],
by = "country_long",
ax = ax,
max_cardinality = 20,
)
Out[19]: <Axes: xlabel='lon', ylabel='lat'>