# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Create a choropleth map with Folium
# 1. Import libraries
# 2. Wrangle data
# 3. Clean data
# 4. Create choropleth map

# # 1. Import libraries

# Import libraries
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib
import os
import folium
import json

# +
# This command propts matplotlib visuals to appear in the notebook 

# %matplotlib inline
# -

# Import Berlin .json file
country_geo = r'/Users/OldBobJulia/Desktop/CF/Course/6. Advanced Analytics and Dashboard Design/Berlin Airbnb Analysis/02 Data/Original data/berlin_bezirke.json'

# Look at it
f = open(r'/Users/OldBobJulia/Desktop/CF/Course/6. Advanced Analytics and Dashboard Design/Berlin Airbnb Analysis/02 Data/Original data/berlin_bezirke.json',)
data = json.load(f)
for i in data['features']:
    print(i)

# File has a geometry column AND a LABEL.  

# Import Airbnb data
df = pd.read_csv(r'/Users/OldBobJulia/Desktop/CF/Course/6. Advanced Analytics and Dashboard Design/Berlin Airbnb Analysis/02 Data/Prepared data/listing_derivedcolumns.csv')

df.head()

# Drop Unnamed: 0 
df = df.drop(columns = ['Unnamed: 0'])

df.head()

df.shape

df['neighbourhood_group'].value_counts(dropna=False)

# # 2. Wrangle data

# Select only necessary columns for choropleth map
columns = ["neighbourhood_group", "latitude", "longitude", "room_type", "price"]

# Create subset with only these columns 
df_2 = df[columns]

df_2.head()

# Make histogram of price to see what price categories could work.
# Make subset of price excluding extreme prices to that end 
price_cat_check = df_2[df_2['price'] < 4000]

price_cat_check.head()

price_cat_check.shape

sns.distplot(price_cat_check['price'], bins = 5)

# Wrangle neighbourhood_group names to fit json data 
df_2['neighbourhood_group'].value_counts(dropna=False)

df_2['neighbourhood_group'] = df_2['neighbourhood_group'].replace({'Charlottenburg-Wilm.': 'Charlottenburg-Wilmersdorf', 'Tempelhof - Schöneberg': 'Tempelhof-Schöneberg', 'Treptow - Köpenick': 'Treptow-Köpenick', 'Steglitz - Zehlendorf': 'Steglitz-Zehlendorf', 'Marzahn - Hellersdorf': 'Marzahn-Hellersdorf'})

df_2.head()

df_2['neighbourhood_group'].value_counts(dropna=False)

# # 3. Check consistency

# Check for missings
df_2.isnull().sum()

# There are no missing values

# Check for duplicates
dups = df_2.duplicated()

dups.shape

# There are no dups

# Check outliers 
df_2.describe()

# 3 observations with prices over 4000 were imputed with the mean previously

# Check how many rows with price under 15
super_low_price = df_2[df_2['price'] < 15]

super_low_price.shape

super_low_price.head()

# I find it surprisingly many, still these rows are less than 10% of the total data set AND they remain within the reasonable price, so I leave them. 

# # 4. Plot choropleth map

data_to_plot = df_2[['neighbourhood_group','price']]
data_to_plot.head()

# +
# Setup a folium map at a high-level zoom
map = folium.Map(location=[52.520, 13.404], width=750, height=500)

folium.Choropleth(
    geo_data = country_geo, 
    data = data_to_plot,
    columns = ['neighbourhood_group', 'price'],
    key_on = 'feature.properties.name',
    fill_color = 'PuBuGn', fill_opacity=0.6, line_opacity=0.1,
    legend_name = "price").add_to(map)
folium.LayerControl().add_to(map)

map
# -

# Save it
map.save("index.html")

# Export as png just in case
map.save("map2.png")

# The map answers the question which neighbourhoods are the most popular ones, or where most money can be made through Airbnb rentals. The answer is Friedrichshain-Kreuzberg, followed by Tempelhof-Schöneberg and Charlottenburg-Wilmersdorf. 
#
# New research question: 
# - Does the number of commercial hosts influence the popularity of a neighbourhood?



StarcoderdataJupyterScriptsDedupFiltered