Created
November 28, 2013 15:44
-
-
Save Nagasaki45/7693916 to your computer and use it in GitHub Desktop.
Countries of the world exercise with pandas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Countries of the world exercise. | |
# download cow.txt and put it in your project directory | |
import pandas as pd | |
def closes_to(data, abbreviated_name): | |
country = data[data.ISO3166A2 == abbreviated_name] | |
latitude = country.BGNc_latitude.values[0] | |
longitude = country.BGNc_longitude.values[0] | |
data['distance_from_{}'.format(abbreviated_name)] = \ | |
((data.BGNc_latitude - latitude) ** 2 + \ | |
(data.BGNc_longitude - longitude) ** 2) ** 0.5 | |
return data.ix[data['distance_from_{}'.format(abbreviated_name)]. \ | |
argsort()[1]] # argsort 1 because there's the input in place 0 | |
data = pd.read_csv('cow.txt', skiprows=28, sep=';') | |
closes_to_israel = closes_to(data, 'IL') | |
print('Closes country to Israel:\n{}'.format(closes_to_israel)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment