# read the shape file world_administrative_boundaries.shp
import geopandas as gpd
# Reading the world shapefile
world_data = gpd.read_file('world-administrative-boundaries.shp')
world_data
iso3 | status | color_code | name | continent | region | iso_3166_1_ | french_shor | geometry | |
---|---|---|---|---|---|---|---|---|---|
0 | None | Adm. by EGY | EGY | Ma'tan al-Sarra | Africa | Northern Africa | None | Ma'tan al-Sarra | POLYGON ((33.25104 21.99977, 34.15064 21.99603... |
1 | CHE | Member State | CHE | Switzerland | Europe | Western Europe | CH | Suisse | POLYGON ((9.56672 47.54045, 9.55980 47.50209, ... |
2 | None | UK Territory | GBR | Jersey | Europe | Northern Europe | None | Jersey | POLYGON ((-2.01500 49.21417, -2.02111 49.17721... |
3 | AUT | Member State | AUT | Austria | Europe | Western Europe | AT | Autriche | POLYGON ((16.94618 48.61907, 16.94333 48.57333... |
4 | PRT | Member State | PRT | Portugal | Europe | Southern Europe | PT | Portugal | POLYGON ((-7.43185 37.25319, -7.41903 37.18055... |
... | ... | ... | ... | ... | ... | ... | ... | ... | ... |
251 | SVK | Member State | SVK | Slovakia | Europe | Eastern Europe | SK | Slovaquie | POLYGON ((22.55805 49.07944, 22.55166 49.03943... |
252 | MLI | Member State | MLI | Mali | Africa | Western Africa | ML | Mali | POLYGON ((-4.80611 25.00027, -4.52528 24.82500... |
253 | ARM | Member State | ARM | Armenia | Asia | Western Asia | AM | Arménie | POLYGON ((46.54038 38.87559, 46.51639 38.87804... |
254 | ALB | Member State | ALB | Albania | Europe | Southern Europe | AL | Albanie | POLYGON ((20.07142 42.56091, 20.10208 42.53347... |
255 | GIB | UK Non-Self-Governing Territory | GBR | Gibraltar | Europe | Southern Europe | GI | Gibraltar | POLYGON ((-5.35580 36.16331, -5.33451 36.16256... |
256 rows × 9 columns
import geopandas as gpd
# Reading the world shapefile
world_data = gpd.read_file(r'world-administrative-boundaries.shp')
world_data.plot()
<Axes: >
# Reading the world shapefile
world_data = gpd.read_file(r'world-administrative-boundaries.shp')
world_data2 = world_data[['name', 'geometry']]
#GeoSeries.area
# Reading the world shapefile
world_data = gpd.read_file(r'world-administrative-boundaries.shp')
world_data2 = world_data[['name', 'geometry']]
# Calculating the area of each country
world_data2['area'] = world_data.area
C:\Users\Ummesalma\AppData\Local\Temp\ipykernel_3720\1599538959.py:8: UserWarning: Geometry is in a geographic CRS. Results from 'area' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation. world_data2['area'] = world_data.area C:\Users\Ummesalma\AppData\Roaming\Python\Python311\site-packages\geopandas\geodataframe.py:1525: 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().__setitem__(key, value)
world_data.info()
<class 'geopandas.geodataframe.GeoDataFrame'> RangeIndex: 256 entries, 0 to 255 Data columns (total 9 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 iso3 238 non-null object 1 status 255 non-null object 2 color_code 255 non-null object 3 name 256 non-null object 4 continent 256 non-null object 5 region 252 non-null object 6 iso_3166_1_ 236 non-null object 7 french_shor 256 non-null object 8 geometry 256 non-null geometry dtypes: geometry(1), object(8) memory usage: 18.1+ KB
world_data2.info()
<class 'geopandas.geodataframe.GeoDataFrame'> RangeIndex: 256 entries, 0 to 255 Data columns (total 3 columns): # Column Non-Null Count Dtype --- ------ -------------- ----- 0 name 256 non-null object 1 geometry 256 non-null geometry 2 area 256 non-null float64 dtypes: float64(1), geometry(1), object(1) memory usage: 6.1+ KB
world_data2.head()
name | geometry | area | |
---|---|---|---|
0 | Ma'tan al-Sarra | POLYGON ((33.25104 21.99977, 34.15064 21.99603... | 0.160197 |
1 | Switzerland | POLYGON ((9.56672 47.54045, 9.55980 47.50209, ... | 4.889680 |
2 | Jersey | POLYGON ((-2.01500 49.21417, -2.02111 49.17721... | 0.015395 |
3 | Austria | POLYGON ((16.94618 48.61907, 16.94333 48.57333... | 10.039437 |
4 | Portugal | POLYGON ((-7.43185 37.25319, -7.41903 37.18055... | 9.302478 |
import geopandas as gpd
# Reading the world shapefile
world_data = gpd.read_file(r'world-administrative-boundaries.shp')
world_data2 = world_data[['name', 'geometry']]
# Calculating the area of each country
world_data2['area'] = world_data.area
# Removing Antarctica from GeoPandas GeoDataframe
world_data2 = world_data[world_data['name'] != 'Antarctica']
world_data2.plot()
C:\Users\Ummesalma\AppData\Local\Temp\ipykernel_3720\3310628924.py:9: UserWarning: Geometry is in a geographic CRS. Results from 'area' are likely incorrect. Use 'GeoSeries.to_crs()' to re-project geometries to a projected CRS before this operation. world_data2['area'] = world_data.area C:\Users\Ummesalma\AppData\Roaming\Python\Python311\site-packages\geopandas\geodataframe.py:1525: 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().__setitem__(key, value)
<Axes: >
world_data2[world_data2.name=="India"].plot()
<Axes: >
# Changing the projection
current_crs = world_data.crs
world_data.to_crs(epsg=3857, inplace=True)
world_data.plot(column='name', cmap='hsv')
<Axes: >
# Re-calculate the areas in Sq. Km.
world_data2['area'] = world_data.area/1000000
# Adding a legend
world_data2.plot(column='area', cmap='hsv', legend=True,
legend_kwds={'label': "Area of the country (Sq. Km.)"},
figsize=(7, 7))
<Axes: >