# Load relevant libraries
import pandas as pd
import numpy as np
import matplotlib
import folium
import geocoder
from tqdm import tqdm
%pylab inline
pylab.rcParams['figure.figsize'] = (10, 8)
# Import pandas
import pandas as pd
# Assign spreadsheet filename: file
file = 'imd_student_blind.xlsx'
# Load spreadsheet: xl
xl = pd.ExcelFile(file)
# Print sheet names
print(xl.sheet_names)
# Load a sheet into a DataFrame by index: df
df = xl.parse(0)
# Print the head of the DataFrame df
df.head()
# Group the data to CEP != 0
df1 = df.loc[df['CEP'] != 0].reset_index(drop = True)
df1.head()
# Group the data to 'nota' >= 7
dfNotasMaior5 = df1.loc[df1['nota'] >= 7].reset_index(drop = True)
dfNotasMaior5.head()
# Group the data to 'nota' < 5
dfNotasMenor5 = df1.loc[df1['nota'] < 5].reset_index(drop = True)
dfNotasMenor5.head()
# We tested the geocoder library with CEP and country as input. Let's create a new column in our dataframe that contains these values.
dfNotasMaior5['geocode_input'] = dfNotasMaior5['CEP'].apply(str) + ', ' + 'Brazil'
# We also create two additional columns for lattitude and longitude.
dfNotasMaior5['lat'], dfNotasMaior5['long'] = [0, 0]
# Display the head of the updated dataframe.
dfNotasMaior5.head()
# Create geocoder with data
for i in tqdm(range(len(dfNotasMaior5))):
g = geocoder.google(dfNotasMaior5.loc[i,'geocode_input'])
dfNotasMaior5.ix[i,'lat'] = g.lat
dfNotasMaior5.ix[i,'long'] = g.lng
print('Geocoding complete!')
# Create cluster with data 'nota' >= 7
import folium
from folium.plugins import HeatMap
# Set map center and zoom level
mapc = [-5.9, -35.2]
zoom = 10
coordinates = []
for i in range(len(dfNotasMaior5)):
# eliminate items with'nan' element
if all(~np.isnan([dfNotasMaior5.ix[i,'lat'], dfNotasMaior5.ix[i,'long'], dfNotasMaior5.ix[i,'a_ID']])):
coordinates.append([dfNotasMaior5.ix[i,'lat'], dfNotasMaior5.ix[i,'long'], dfNotasMaior5.ix[i,'a_ID']])
# Create map object
m = folium.Map(location=mapc,
zoom_start=zoom)
# Create cluster
marker_cluster = folium.MarkerCluster().add_to(m)
# Plot each of the locations that we geocoded
for j in tqdm(range(len(dfNotasMaior5))):
folium.Marker([dfNotasMaior5.ix[j,'lat'], dfNotasMaior5.ix[j,'long']],
icon=folium.Icon(color='green',icon='info-sign')
).add_to(marker_cluster)
m
# We tested the geocoder library with CEP and country as input. Let's create a new column in our dataframe that contains these values.
dfNotasMenor5['geocode_input'] = dfNotasMenor5['CEP'].apply(str) + ', ' + 'Brazil'
# We also create two additional columns for lattitude and longitude.
dfNotasMenor5['lat'], dfNotasMenor5['long'] = [0, 0]
# Display the head of the updated dataframe.
dfNotasMenor5.head()
# Create geocoder with data
for i in tqdm(range(len(dfNotasMenor5))):
g = geocoder.google(dfNotasMenor5.loc[i,'geocode_input'])
dfNotasMenor5.ix[i,'lat'] = g.lat
dfNotasMenor5.ix[i,'long'] = g.lng
print('Geocoding complete!')
# Create cluster with data 'nota' >= 7
import folium
from folium.plugins import HeatMap
# Set map center and zoom level
mapc = [-5.9, -35.2]
zoom = 10
coordinates = []
for i in range(len(dfNotasMenor5)):
# eliminate items with'nan' element
if all(~np.isnan([dfNotasMenor5.ix[i,'lat'], dfNotasMenor5.ix[i,'long'], dfNotasMenor5.ix[i,'a_ID']])):
coordinates.append([dfNotasMenor5.ix[i,'lat'], dfNotasMenor5.ix[i,'long'], dfNotasMenor5.ix[i,'a_ID']])
# Create map object
m = folium.Map(location=mapc,
zoom_start=zoom)
marker_cluster = folium.MarkerCluster().add_to(m)
#folium.Marker(
# location=[coordinates],
# popup='Add popup text here.',
# icon=folium.Icon(color='green', icon='ok-sign'),
#).add_to(marker_cluster)
# Plot each of the locations that we geocoded
for j in tqdm(range(len(dfNotasMenor5))):
folium.Marker([dfNotasMenor5.ix[j,'lat'], dfNotasMenor5.ix[j,'long']],
icon=folium.Icon(color='green',icon='info-sign')
).add_to(marker_cluster)
#HeatMap(coordinates).add_to(m)
m