You are on page 1of 4

Experiment No : 05

Name: Anjali phalke

Roll No: 2015056

In [ ]: !pip install geopandas

Looking in indexes: https://pypi.org/simple, (https://pypi.org/simple,) https://us-python.pkg.dev/colab-wheels/public/simple/ (https://us-python.pkg.dev/colab-wheels/publi


c/simple/)
Collecting geopandas
Downloading geopandas-0.12.2-py3-none-any.whl (1.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.1/1.1 MB 7.5 MB/s eta 0:00:00
Requirement already satisfied: packaging in /usr/local/lib/python3.9/dist-packages (from geopandas) (23.1)
Requirement already satisfied: shapely>=1.7 in /usr/local/lib/python3.9/dist-packages (from geopandas) (2.0.1)
Collecting fiona>=1.8
Downloading Fiona-1.9.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 16.1/16.1 MB 24.3 MB/s eta 0:00:00
Requirement already satisfied: pandas>=1.0.0 in /usr/local/lib/python3.9/dist-packages (from geopandas) (1.5.3)
Collecting pyproj>=2.6.1.post1
Downloading pyproj-3.5.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (7.8 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 7.8/7.8 MB 24.7 MB/s eta 0:00:00
Collecting cligj>=0.5
Downloading cligj-0.7.2-py3-none-any.whl (7.1 kB)
Collecting munch>=2.3.2
Downloading munch-2.5.0-py2.py3-none-any.whl (10 kB)
Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.9/dist-packages (from fiona>=1.8->geopandas) (6.4.1)
Collecting click-plugins>=1.0
Downloading click_plugins-1.1.1-py2.py3-none-any.whl (7.5 kB)
Requirement already satisfied: click~=8.0 in /usr/local/lib/python3.9/dist-packages (from fiona>=1.8->geopandas) (8.1.3)
Requirement already satisfied: attrs>=19.2.0 in /usr/local/lib/python3.9/dist-packages (from fiona>=1.8->geopandas) (23.1.0)
Requirement already satisfied: certifi in /usr/local/lib/python3.9/dist-packages (from fiona>=1.8->geopandas) (2022.12.7)
Requirement already satisfied: python-dateutil>=2.8.1 in /usr/local/lib/python3.9/dist-packages (from pandas>=1.0.0->geopandas) (2.8.2)
Requirement already satisfied: numpy>=1.20.3 in /usr/local/lib/python3.9/dist-packages (from pandas>=1.0.0->geopandas) (1.22.4)
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.9/dist-packages (from pandas>=1.0.0->geopandas) (2022.7.1)
Requirement already satisfied: six in /usr/local/lib/python3.9/dist-packages (from munch>=2.3.2->fiona>=1.8->geopandas) (1.16.0)
Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.9/dist-packages (from importlib-metadata->fiona>=1.8->geopandas) (3.15.0)
Installing collected packages: pyproj, munch, cligj, click-plugins, fiona, geopandas
Successfully installed click-plugins-1.1.1 cligj-0.7.2 fiona-1.9.3 geopandas-0.12.2 munch-2.5.0 pyproj-3.5.0

In [ ]: !pip install geoplot

Looking in indexes: https://pypi.org/simple, (https://pypi.org/simple,) https://us-python.pkg.dev/colab-wheels/public/simple/ (https://us-python.pkg.dev/colab-wheels/publi


c/simple/)
Collecting geoplot
Downloading geoplot-0.5.1-py3-none-any.whl (28 kB)
Requirement already satisfied: geopandas>=0.9.0 in /usr/local/lib/python3.9/dist-packages (from geoplot) (0.12.2)
Requirement already satisfied: seaborn in /usr/local/lib/python3.9/dist-packages (from geoplot) (0.12.2)
Collecting contextily>=1.0.0
Downloading contextily-1.3.0-py3-none-any.whl (16 kB)
Collecting cartopy
Downloading Cartopy-0.21.1.tar.gz (10.9 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 10.9/10.9 MB 68.0 MB/s eta 0:00:00
Installing build dependencies ... done
Getting requirements to build wheel ... done
Preparing metadata (pyproject.toml) ... done
Requirement already satisfied: matplotlib>=3.1.2 in /usr/local/lib/python3.9/dist-packages (from geoplot) (3.7.1)
Collecting mapclassify>=2.1
Downloading mapclassify-2.5.0-py3-none-any.whl (39 kB)
Requirement already satisfied: pandas in /usr/local/lib/python3.9/dist-packages (from geoplot) (1.5.3)
Requirement already satisfied: geopy in /usr/local/lib/python3.9/dist-packages (from contextily>=1.0.0->geoplot) (2.3.0)
Collecting rasterio
Downloading rasterio-1.3.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (20.1 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 20.1/20.1 MB 19.3 MB/s eta 0:00:00
Requirement already satisfied: requests in /usr/local/lib/python3.9/dist-packages (from contextily>=1.0.0->geoplot) (2.27.1)
Collecting xyzservices
Downloading xyzservices-2023.2.0-py3-none-any.whl (55 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 55.4/55.4 kB 4.9 MB/s eta 0:00:00
Collecting mercantile
Downloading mercantile-1.2.1-py3-none-any.whl (14 kB)
Requirement already satisfied: joblib in /usr/local/lib/python3.9/dist-packages (from contextily>=1.0.0->geoplot) (1.2.0)
Requirement already satisfied: pillow in /usr/local/lib/python3.9/dist-packages (from contextily>=1.0.0->geoplot) (8.4.0)
Requirement already satisfied: packaging in /usr/local/lib/python3.9/dist-packages (from geopandas>=0.9.0->geoplot) (23.1)
Requirement already satisfied: shapely>=1.7 in /usr/local/lib/python3.9/dist-packages (from geopandas>=0.9.0->geoplot) (2.0.1)
Requirement already satisfied: pyproj>=2.6.1.post1 in /usr/local/lib/python3.9/dist-packages (from geopandas>=0.9.0->geoplot) (3.5.0)
Requirement already satisfied: fiona>=1.8 in /usr/local/lib/python3.9/dist-packages (from geopandas>=0.9.0->geoplot) (1.9.3)
Requirement already satisfied: networkx in /usr/local/lib/python3.9/dist-packages (from mapclassify>=2.1->geoplot) (3.1)
Requirement already satisfied: numpy>=1.3 in /usr/local/lib/python3.9/dist-packages (from mapclassify>=2.1->geoplot) (1.22.4)
Requirement already satisfied: scikit-learn in /usr/local/lib/python3.9/dist-packages (from mapclassify>=2.1->geoplot) (1.2.2)
Requirement already satisfied: scipy>=1.0 in /usr/local/lib/python3.9/dist-packages (from mapclassify>=2.1->geoplot) (1.10.1)
Requirement already satisfied: importlib-resources>=3.2.0 in /usr/local/lib/python3.9/dist-packages (from matplotlib>=3.1.2->geoplot) (5.12.0)
Requirement already satisfied: pyparsing>=2.3.1 in /usr/local/lib/python3.9/dist-packages (from matplotlib>=3.1.2->geoplot) (3.0.9)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.9/dist-packages (from matplotlib>=3.1.2->geoplot) (1.4.4)
Requirement already satisfied: python-dateutil>=2.7 in /usr/local/lib/python3.9/dist-packages (from matplotlib>=3.1.2->geoplot) (2.8.2)
Requirement already satisfied: fonttools>=4.22.0 in /usr/local/lib/python3.9/dist-packages (from matplotlib>=3.1.2->geoplot) (4.39.3)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.9/dist-packages (from matplotlib>=3.1.2->geoplot) (0.11.0)
Requirement already satisfied: contourpy>=1.0.1 in /usr/local/lib/python3.9/dist-packages (from matplotlib>=3.1.2->geoplot) (1.0.7)
Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.9/dist-packages (from pandas->geoplot) (2022.7.1)
Collecting pyshp>=2.1
Downloading pyshp-2.3.1-py2.py3-none-any.whl (46 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 46.5/46.5 kB 5.0 MB/s eta 0:00:00
Requirement already satisfied: cligj>=0.5 in /usr/local/lib/python3.9/dist-packages (from fiona>=1.8->geopandas>=0.9.0->geoplot) (0.7.2)
Requirement already satisfied: click~=8.0 in /usr/local/lib/python3.9/dist-packages (from fiona>=1.8->geopandas>=0.9.0->geoplot) (8.1.3)
Requirement already satisfied: attrs>=19.2.0 in /usr/local/lib/python3.9/dist-packages (from fiona>=1.8->geopandas>=0.9.0->geoplot) (23.1.0)
Requirement already satisfied: certifi in /usr/local/lib/python3.9/dist-packages (from fiona>=1.8->geopandas>=0.9.0->geoplot) (2022.12.7)
Requirement already satisfied: click-plugins>=1.0 in /usr/local/lib/python3.9/dist-packages (from fiona>=1.8->geopandas>=0.9.0->geoplot) (1.1.1)
Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.9/dist-packages (from fiona>=1.8->geopandas>=0.9.0->geoplot) (6.4.1)
Requirement already satisfied: munch>=2.3.2 in /usr/local/lib/python3.9/dist-packages (from fiona>=1.8->geopandas>=0.9.0->geoplot) (2.5.0)
Requirement already satisfied: zipp>=3.1.0 in /usr/local/lib/python3.9/dist-packages (from importlib-resources>=3.2.0->matplotlib>=3.1.2->geoplot) (3.15.0)
Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.9/dist-packages (from python-dateutil>=2.7->matplotlib>=3.1.2->geoplot) (1.16.0)
Requirement already satisfied: geographiclib<3,>=1.52 in /usr/local/lib/python3.9/dist-packages (from geopy->contextily>=1.0.0->geoplot) (2.0)
Requirement already satisfied: setuptools in /usr/local/lib/python3.9/dist-packages (from rasterio->contextily>=1.0.0->geoplot) (67.6.1)
Collecting affine
Downloading affine-2.4.0-py3-none-any.whl (15 kB)
Collecting snuggs>=1.4.1
Downloading snuggs-1.4.7-py3-none-any.whl (5.4 kB)
Requirement already satisfied: charset-normalizer~=2.0.0 in /usr/local/lib/python3.9/dist-packages (from requests->contextily>=1.0.0->geoplot) (2.0.12)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.9/dist-packages (from requests->contextily>=1.0.0->geoplot) (3.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.9/dist-packages (from requests->contextily>=1.0.0->geoplot) (1.26.15)
Requirement already satisfied: threadpoolctl>=2.0.0 in /usr/local/lib/python3.9/dist-packages (from scikit-learn->mapclassify>=2.1->geoplot) (3.1.0)
Building wheels for collected packages: cartopy
Building wheel for cartopy (pyproject.toml) ... done
Created wheel for cartopy: filename=Cartopy-0.21.1-cp39-cp39-linux_x86_64.whl size=11113644 sha256=7f65ab08420fde6e93e1b07dc5bd639135b15ad6412a94611728a0d42e73715c
Stored in directory: /root/.cache/pip/wheels/74/b9/f5/2c94acd7cd21480e6cf63169144d7aac3e8d9cf638225ed578
Successfully built cartopy
Installing collected packages: xyzservices, snuggs, pyshp, mercantile, affine, rasterio, mapclassify, contextily, cartopy, geoplot
Successfully installed affine-2.4.0 cartopy-0.21.1 contextily-1.3.0 geoplot-0.5.1 mapclassify-2.5.0 mercantile-1.2.1 pyshp-2.3.1 rasterio-1.3.6 snuggs-1.4.7 xyzservices-20
23.2.0
In [ ]: import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import geopandas as gpd

%matplotlib inline

In [ ]: from osgeo import gdal

In [ ]: world_data = gpd.read_file(r'world.shp')

world_data

Out[5]: FIPS ISO2 ISO3 UN NAME AREA POP2005 REGION SUBREGION LON LAT geometry

0 AC AG ATG 28 Antigua and Barbuda 44 83039 19 29 -61.783 17.078 MULTIPOLYGON (((-61.68667 17.02444, -61.73806 ...

1 AG DZ DZA 12 Algeria 238174 32854159 2 15 2.632 28.163 POLYGON ((2.96361 36.80222, 2.98139 36.80694, ...

2 AJ AZ AZE 31 Azerbaijan 8260 8352021 142 145 47.395 40.430 MULTIPOLYGON (((45.08332 39.76804, 45.26639 39...

3 AL AL ALB 8 Albania 2740 3153731 150 39 20.068 41.143 POLYGON ((19.43621 41.02107, 19.45055 41.06000...

4 AM AM ARM 51 Armenia 2820 3017661 142 145 44.563 40.534 MULTIPOLYGON (((45.57305 40.63249, 45.52888 40...

... ... ... ... ... ... ... ... ... ... ... ... ...

241 TB BL BLM 652 Saint Barthelemy 0 0 19 29 -63.043 18.040 POLYGON ((-63.02834 18.01555, -63.03334 18.015...

242 GK GG GGY 831 Guernsey 0 0 150 154 -2.576 49.459 POLYGON ((-2.59083 49.42249, -2.59722 49.42249...

243 JE JE JEY 832 Jersey 0 0 150 154 -2.129 49.219 POLYGON ((-2.01500 49.21416, -2.02111 49.17722...

244 SX GS SGS 239 South Georgia South Sandwich Islands 0 0 0 0 -36.891 -54.209 MULTIPOLYGON (((-27.32584 -59.42722, -27.29806...
245 TW TW TWN 158 Taiwan 0 0 0 0 120.946 23.754 MULTIPOLYGON (((121.57639 22.00139, 121.57027 ...

246 rows × 12 columns

world_data.plot()
In [ ]:

Out[6]: <Axes: >

In [ ]: world_data1 = world_data[['NAME','geometry']]
world_data1

Out[7]: NAME geometry

0 Antigua and Barbuda MULTIPOLYGON (((-61.68667 17.02444, -61.73806 ...

1 Algeria POLYGON ((2.96361 36.80222, 2.98139 36.80694, ...

2 Azerbaijan MULTIPOLYGON (((45.08332 39.76804, 45.26639 39...

3 Albania POLYGON ((19.43621 41.02107, 19.45055 41.06000...

4 Armenia MULTIPOLYGON (((45.57305 40.63249, 45.52888 40...

... ... ...

241 Saint Barthelemy POLYGON ((-63.02834 18.01555, -63.03334 18.015...

242 Guernsey POLYGON ((-2.59083 49.42249, -2.59722 49.42249...

243 Jersey POLYGON ((-2.01500 49.21416, -2.02111 49.17722...

244 South Georgia South Sandwich Islands MULTIPOLYGON (((-27.32584 -59.42722, -27.29806...

245 Taiwan MULTIPOLYGON (((121.57639 22.00139, 121.57027 ...

246 rows × 2 columns

In [ ]: world_data1['area']=world_data.area

<ipython-input-8-24e460c98e5b>:1: 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_data1['area']=world_data.area
/usr/local/lib/python3.9/dist-packages/geopandas/geodataframe.py:1443: 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 (https://pandas.pydata.org/panda


s-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy)
super(). setitem (key, value)

In [ ]: world_data1 = world_data1[world_data1['NAME']!= 'Antarctica']


world_data1

Out[9]: NAME geometry area

0 Antigua and Barbuda MULTIPOLYGON (((-61.68667 17.02444, -61.73806 ... 0.046183

1 Algeria POLYGON ((2.96361 36.80222, 2.98139 36.80694, ... 213.512502

2 Azerbaijan MULTIPOLYGON (((45.08332 39.76804, 45.26639 39... 9.100912

3 Albania POLYGON ((19.43621 41.02107, 19.45055 41.06000... 3.075918

4 Armenia MULTIPOLYGON (((45.57305 40.63249, 45.52888 40... 3.142093

... ... ... ...

241 Saint Barthelemy POLYGON ((-63.02834 18.01555, -63.03334 18.015... 0.004696

242 Guernsey POLYGON ((-2.59083 49.42249, -2.59722 49.42249... 0.009359

243 Jersey POLYGON ((-2.01500 49.21416, -2.02111 49.17722... 0.015408

244 South Georgia South Sandwich Islands MULTIPOLYGON (((-27.32584 -59.42722, -27.29806... 0.542074

245 Taiwan MULTIPOLYGON (((121.57639 22.00139, 121.57027 ... 3.221167

245 rows × 3 columns


In [ ]: current_crs = world_data.crs
world_data1.to_crs(epsg=3857, inplace=True)

world_data1.plot(column='NAME', cmap='hsv')

/usr/local/lib/python3.9/dist-packages/geopandas/geodataframe.py:1443: 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 (https://pandas.pydata.org/panda


s-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy)
super(). setitem (key, value)

Out[10]: <Axes: >

In [ ]: world_data1.plot(column='area', cmap='hsv')

Out[11]: <Axes: >

In [ ]: world_data1['area']=world_data1.area/1000000

/usr/local/lib/python3.9/dist-packages/geopandas/geodataframe.py:1443: 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 (https://pandas.pydata.org/panda


s-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy)
super(). setitem (key, value)

In [ ]: world_data1.plot(column='area', cmap='hsv',legend=True, legend_kwds= {'label': "Area of the Country (Sq. Km.)"},


figsize=(7,7))

Out[13]: <Axes: >


In [ ]: df = pd.read_csv('/content/IndianMigrationHistory.csv')

In [ ]: df.rename(columns={'Country Origin Name': 'conorname',


'Migration by Gender Code':'migencode',
'Country Dest Name':'condesname',
'1960 [1960]':'1960',
'1970 [1970]':'1970',
'1980 [1980]':'1980',
'1990 [1990]':'1990',
'2000 [2000]':'2000'},inplace=True)

In [ ]: df_new=df[['migencode','condesname','1960', '1970', '1980','1990', '2000']]


df_new.head()

Out[17]: migencode condesname 1960 1970 1980 1990 2000

0 FEM Canada 3779 22178 41574 64890 145504

1 FEM Australia 4650 8782 19580 29547 44315

2 FEM Denmark 373 234 943 1565 2556

3 FEM Finland 12 49 90 348 424


4 FEM France 38575 3919 7211 10838 69004

In [ ]: data = df_new[df_new['migencode']=='TOT'][['condesname','2000']]
data

Out[18]: condesname 2000

26 Canada 313999

27 Australia 94110

28 Denmark 4400

29 Finland 1136

30 France 136233

31 Germany 35486

32 Netherlands 10834

33 New Zealand 20667

34 Singapore 105187

35 Sweden 10988

36 Switzerland 6419

37 United Kingdom 524796


38 United States 1041320

In [ ]: merged_df = world_data.merge(data, left_on='NAME', right_on='condesname')


merged_df.head()

Out[19]: FIPS ISO2 ISO3 UN NAME AREA POP2005 REGION SUBREGION LON LAT geometry condesname 2000

0 AS AU AUS 36 Australia 768230 20310208 9 53 136.189 -24.973 MULTIPOLYGON (((158.88217 -54.71139, 158.87967... Australia 94110

1 CA CA CAN 124 Canada 909351 32270507 19 21 -109.433 59.081 MULTIPOLYGON (((-65.61362 43.42027, -65.61972 ... Canada 313999

2 DA DK DNK 208 Denmark 4243 5416945 150 154 9.264 56.058 MULTIPOLYGON (((11.51389 54.82972, 11.56444 54... Denmark 4400

3 FI FI FIN 246 Finland 30459 5246004 150 154 26.272 64.504 MULTIPOLYGON (((23.70583 59.92722, 23.64944 59... Finland 1136
4 FR FR FRA 250 France 55010 60990544 150 155 2.550 46.565 MULTIPOLYGON (((9.48583 42.61527, 9.49472 42.6... France 136233

In [ ]: merged_df.plot(column='2000', cmap='hsv',legend=True,
legend_kwds={'label': 'Density of Emigrants'},
figsize=(7,7))
plt.title('Indian migrants by Country (Year 2000)')
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.xlim(-180.433,180.235)
plt.ylim(-55.634,90.504)
plt.show()

You might also like