Skip to content

Instantly share code, notes, and snippets.

@Nagasaki45
Created November 28, 2013 15:44
Show Gist options
  • Save Nagasaki45/7693916 to your computer and use it in GitHub Desktop.
Save Nagasaki45/7693916 to your computer and use it in GitHub Desktop.
Countries of the world exercise with pandas
# 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