Pandas ve GeoPy Kullanımı

GeoPy yüklemek için komut satırına; Untitled1
In [4]:
import geopy # *** internet başlantısı gerekiyor.
dir(geopy)
Out[4]:
['ArcGIS',
 'Baidu',
 'Bing',
 'DataBC',
 'GeoNames',
 'GeocodeFarm',
 'GeocoderDotUS',
 'GoogleV3',
 'IGNFrance',
 'LiveAddress',
 'Location',
 'NaviData',
 'Nominatim',
 'OpenCage',
 'OpenMapQuest',
 'Photon',
 'Point',
 'What3Words',
 'YahooPlaceFinder',
 'Yandex',
 '__builtins__',
 '__cached__',
 '__doc__',
 '__file__',
 '__loader__',
 '__name__',
 '__package__',
 '__path__',
 '__spec__',
 '__version__',
 'compat',
 'exc',
 'format',
 'geocoders',
 'get_geocoder_for_service',
 'location',
 'point',
 'units',
 'util']
In [7]:
from geopy.geocoders import Nominatim
nom=Nominatim(scheme="http")
In [13]:
print( nom.geocode("3995 23rd St, San Francisco, CA 94114") )
print( nom.geocode("Mersin University") )
3995, 23rd Street, Noe Valley, SF, California, 94114, United States of America
Meslek Yüksek Okulu, 33-59, Gülnar, Mersin, Akdeniz Bölgesi, Türkiye
In [14]:
import os 
os.listdir()
Out[14]:
['.ipynb_checkpoints',
 'pandasDataProcess.html',
 'supermarkets-commas.txt',
 'supermarkets-semi-colons.txt',
 'supermarkets.csv',
 'supermarkets.json',
 'supermarkets.xlsx',
 'Untitled.ipynb',
 'Untitled1.ipynb']
In [16]:
import pandas
df=pandas.read_csv("supermarkets.csv");
df
Out[16]:
ID Address City State Country Name Employees
0 1 3666 21st St San Francisco CA 94114 USA Madeira 8
1 2 735 Dolores St San Francisco CA 94119 USA Bready Shop 15
2 3 332 Hill St San Francisco California 94114 USA Super River 25
3 4 3995 23rd St San Francisco CA 94114 USA Ben's Shop 10
4 5 1056 Sanchez St San Francisco California USA Sanchez 12
5 6 551 Alvarado St San Francisco CA 94114 USA Richvalley 20
In [18]:
df["Address"]=df["Address"]+", "+df["City"]+", "+df["State"]+", "+df["Country"]
df
Out[18]:
ID Address City State Country Name Employees
0 1 3666 21st St, San Francisco, CA 94114, USA, Sa... San Francisco CA 94114 USA Madeira 8
1 2 735 Dolores St, San Francisco, CA 94119, USA, ... San Francisco CA 94119 USA Bready Shop 15
2 3 332 Hill St, San Francisco, California 94114, ... San Francisco California 94114 USA Super River 25
3 4 3995 23rd St, San Francisco, CA 94114, USA, Sa... San Francisco CA 94114 USA Ben's Shop 10
4 5 1056 Sanchez St, San Francisco, California, US... San Francisco California USA Sanchez 12
5 6 551 Alvarado St, San Francisco, CA 94114, USA,... San Francisco CA 94114 USA Richvalley 20
In [20]:
# Tabloya koordinat bilgi ekleyeceğim.
# eğer internet bağlantısı kurulamazsa uzuuuuuun bir hata alırsınız.
df["Coordinates"]=df["Address"].apply(nom.geocode)
df
Out[20]:
ID Address City State Country Name Employees Coordinates
0 1 3666 21st St, San Francisco, CA 94114, USA, Sa... San Francisco CA 94114 USA Madeira 8 (3666, 21st Street, Noe Valley, SF, California...
1 2 735 Dolores St, San Francisco, CA 94119, USA, ... San Francisco CA 94119 USA Bready Shop 15 None
2 3 332 Hill St, San Francisco, California 94114, ... San Francisco California 94114 USA Super River 25 (332, Hill Street, Noe Valley, SF, California,...
3 4 3995 23rd St, San Francisco, CA 94114, USA, Sa... San Francisco CA 94114 USA Ben's Shop 10 (3995, 23rd Street, Noe Valley, SF, California...
4 5 1056 Sanchez St, San Francisco, California, US... San Francisco California USA Sanchez 12 (1056, Sanchez Street, Noe Valley, SF, Califor...
5 6 551 Alvarado St, San Francisco, CA 94114, USA,... San Francisco CA 94114 USA Richvalley 20 (551, Alvarado Street, Noe Valley, SF, Califor...
In [34]:
dir(df.Coordinates[0])
Out[34]:
['__class__',
 '__delattr__',
 '__dir__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__gt__',
 '__hash__',
 '__init__',
 '__init_subclass__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__module__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__setattr__',
 '__sizeof__',
 '__slots__',
 '__str__',
 '__subclasshook__',
 '__unicode__',
 '_address',
 '_point',
 '_raw',
 '_tuple',
 'address',
 'altitude',
 'latitude',
 'longitude',
 'point',
 'raw']
In [35]:
df.Coordinates[0].latitude
Out[35]:
37.756488877551
In [36]:
# 1. satır Coordinates özelliği "None" olduğundan hata alacaksın.
# "if x!=None else None" ifadesi None değil ise geç None ise None olarak yaz demektir.
df["Latitude"]=df["Coordinates"].apply(lambda x: x.latitude if x!=None else None)
df["Longitude"]=df["Coordinates"].apply(lambda x: x.longitude if x!=None else None)
df["Altitude"]=df["Coordinates"].apply(lambda x: x.altitude if x!=None else None)

df
Out[36]:
ID Address City State Country Name Employees Coordinates Latitude Longitude Altitude
0 1 3666 21st St, San Francisco, CA 94114, USA, Sa... San Francisco CA 94114 USA Madeira 8 (3666, 21st Street, Noe Valley, SF, California... 37.756489 -122.429343 0.0
1 2 735 Dolores St, San Francisco, CA 94119, USA, ... San Francisco CA 94119 USA Bready Shop 15 None NaN NaN NaN
2 3 332 Hill St, San Francisco, California 94114, ... San Francisco California 94114 USA Super River 25 (332, Hill Street, Noe Valley, SF, California,... 37.755725 -122.428601 0.0
3 4 3995 23rd St, San Francisco, CA 94114, USA, Sa... San Francisco CA 94114 USA Ben's Shop 10 (3995, 23rd Street, Noe Valley, SF, California... 37.752965 -122.431714 0.0
4 5 1056 Sanchez St, San Francisco, California, US... San Francisco California USA Sanchez 12 (1056, Sanchez Street, Noe Valley, SF, Califor... 37.752146 -122.429815 0.0
5 6 551 Alvarado St, San Francisco, CA 94114, USA,... San Francisco CA 94114 USA Richvalley 20 (551, Alvarado Street, Noe Valley, SF, Califor... 37.753673 -122.433220 0.0

pip install geopy 

yada

pip3 install geopy

yazmanız yeterlidir.
geopy internete bağlantısı ister. İnternet bağlantısı yoksa kullanırken uzun hatlar alabilirsiniz.

Yorumlar

Popüler Yayınlar