In [1]:
import pandas as pd
import numpy as np
In [2]:
df = pd.read_csv("ch_postal_codes_utf8.csv")
In [3]:
print(df.shape)
(3430, 8)
In [4]:
print(df.columns)
Index(['Postal Code', 'Place Name', 'State', 'State Abbreviation', 'County',
       'City', 'Latitude', 'Longitude'],
      dtype='object')
In [5]:
print(df.head())
   Postal Code  Place Name          State State Abbreviation        County  \
0         5000       Aarau  Kanton Aargau                 AG  Bezirk Aarau   
1         5001       Aarau  Kanton Aargau                 AG  Bezirk Aarau   
2         5004       Aarau  Kanton Aargau                 AG  Bezirk Aarau   
3         5017  Barmelweid  Kanton Aargau                 AG  Bezirk Aarau   
4         5018  Erlinsbach  Kanton Aargau                 AG  Bezirk Aarau   

              City  Latitude  Longitude  
0            Aarau   47.3890     8.0487  
1            Aarau   47.3922     8.0497  
2            Aarau   47.4005     8.0606  
3  Erlinsbach (AG)   47.4216     7.9700  
4  Erlinsbach (AG)   47.4126     8.0089  
In [6]:
df["State"].value_counts()
Out[6]:
Kanton Bern                      488
Canton de Vaud                   340
Kanton Zürich                    317
Kanton Graubünden                273
Kanton Aargau                    247
Cantone Ticino                   244
Kanton St. Gallen                191
Canton de Fribourg               180
Canton du Valais                 172
Kanton Luzern                    140
Kanton Solothurn                 128
Kanton Thurgau                   126
Kanton Basel-Landschaft           90
Canton de Neuchâtel               81
Canton du Jura                    78
Canton de Genève                  64
Kanton Basel-Stadt                60
Kanton Schwyz                     50
Kanton Schaffhausen               40
Kanton Appenzell Ausserrhoden     28
Kanton Uri                        26
Kanton Zug                        23
Kanton Obwalden                   18
Kanton Nidwalden                  15
Kanton Appenzell Innerrhoden       7
Kanton Glarus                      4
Name: State, dtype: int64
In [7]:
df["City"].value_counts()
Out[7]:
Zürich            60
Basel             58
Bern              27
Lugano            25
St. Gallen        22
                  ..
Waldstatt          1
Schmitten (FR)     1
Gimel              1
Céligny            1
Schönengrund       1
Name: City, Length: 2294, dtype: int64
In [8]:
lat = np.array(df["Latitude"])
lng = np.array(df["Longitude"])
dlat = (lat[...,np.newaxis]-lat)*np.pi/180.
dlng = (lng[...,np.newaxis]-lng)*np.pi/180.
re = 6378.137 # The radius of Earth in km
In [9]:
d = 2*re*np.arcsin((np.sin(dlat*0.5)**2+np.cos(lat)*np.cos(lat[...,np.newaxis])*np.sin(dlng*0.5)**2)**0.5)
In [10]:
d.max()
Out[10]:
438.6792425270124
In [11]:
d[d>0].min()
Out[11]:
0.011131949079696899
In [12]:
ix_gr = np.array(df[df["State Abbreviation"]=="GR"].index)
ix_zh = np.array(df[df["State Abbreviation"]=="ZH"].index)
In [13]:
d_grzh = d[ix_gr[...,np.newaxis],ix_zh]
In [14]:
d_grzh.min()
Out[14]:
51.5451101528211
In [15]:
d_grzh.max()
Out[15]:
230.62165308850706
In [ ]: