Final Exam_Python
IMPORTANT: for all visualizations, use your netID as the title; otherwise they will
not be graded.
1. Please load the “NYC2023_AirBNB” dataset into Python using the following code. The
dataset is downloadable along with this task.
import pandas as pd
df = pd.read_csv(r"path_to_the_data\NYC2023_AirBNB.csv")
Make sure you use the correct path.
Below is a description of each field in this dataset:
Field Name Description
host_id Unique identifier for the host.
host_name Name of the host.
neighbourhood_group Borough where the listing is located (e.g., Manhattan,
Brooklyn).
neighbourhood Specific neighborhood within the borough.
latitude Geographic latitude of the listing.
longitude Geographic longitude of the listing.
room_type Type of accommodation (e.g., Entire home/apt, Private
room).
price Average Cost per night in USD.
minimum_nights Minimum stay requirement for the listing.
number_of_reviews Total number of reviews received.
last_review Date of the most recent review.
reviews_per_month Average number of reviews per month.
calculated_host_listings_count Total listings owned by the host.
availability_365 Number of available days in a year.
Each observation in the dataset represents a different property, and the price is the
average booking price for that property. Before proceeding, remove observations where
the price exceeds the 95th percentile using the following code:
price_95 = df['price'].quantile(0.95)
df = df[df['price'] <= price_95]
Then visualize the price distribution across different “neighbourhood_group” using a
violin plot.
(10%).
import seaborn as sns
import [Link] as plt
[Link](figsize=(10, 6))
[Link](x='neighbourhood_group', y='price', data=df)
[Link]('cs8366')
[Link]('Price (USD)')
[Link]('Borough')
plt.tight_layout()
[Link]()
2. Based on your observations from Task 1, please answer which neighborhood commands a
higher average rate? (5%).
Manhattan has the highest median line in the violin
3. There is a debate regarding the location of AirBNB properties: some argue that properties
located farther from the town center command higher prices, as people often book
AirBNBs for getaways, seeking more secluded areas. On the other hand, others believe
that properties closer to the town center command higher prices due to the added
convenience of being near amenities and attractions. In this analysis, we aim to
investigate whether properties located farther from the center of their district tend to have
higher or lower prices. To achieve this, we first use the following code to load the
latitude and longitude of the center of each district.
district_centers = {
'Manhattan': {'latitude': 40.7831, 'longitude': -73.9712},
'Brooklyn': {'latitude': 40.6782, 'longitude': -73.9442},
'Queens': {'latitude': 40.7282, 'longitude': -73.7949},
'Staten Island': {'latitude': 40.5795, 'longitude': -74.1502},
'Bronx': {'latitude': 40.8448, 'longitude': -73.8648}
}
And then compute the distance between each property to its district center using
Euclidean distance. You may use the code below:
def calculate_distance(row):
district = row['neighbourhood_group']
host_lat = row['latitude']
host_lon = row['longitude']
# Get the center coordinates for the given district
center_lat = district_centers[district]['latitude']
center_lon = district_centers[district]['longitude']
# Calculate Euclidean distance
distance = ((host_lat - center_lat) ** 2 + (host_lon - center_lon) ** 2) ** 0.5
return distance
df['distance_from_center'] = [Link](calculate_distance, axis=1)
Now we have a new column “distance_from_center”. Lastly, we need to compute the
logarithm of the price to smooth this variable. You may use the following code.
import math
df['log_price'] = df['price'].apply(lambda x: [Link](x) if x > 0 else 0)
Now we have another new column “log_price”, which is the logarithm of the price.
Please choose a proper visualization method to visualize the relationship between
“log_price” and “distance_from_center”. (10%)
[Link](figsize=(10, 6))
[Link](x='distance_from_center', y='log_price', hue='neighbourhood_group', data=df,
alpha=0.5)
[Link]('cs8366')
[Link]('Distance from Center')
[Link]('Log Price')
plt.tight_layout()
[Link]()
4. Based on the visualization in the previous question, what is your finding? Whether
properties located farther from the center of their district tend to have higher or lower
prices? Or maybe there is no obvious relationship? (5%)
Prices decrease as distance increases
5. While price is an important consideration, the number of bookings also plays a critical
role in determining revenue. A property with a very high price but very few bookings
will still generate low revenue. To estimate the total revenue generated by each property,
we can multiply the average price ("price") by the number of bookings
("number_of_reviews"), assuming that each booking corresponds to a review.
Please begin by calculating a new column called "revenue," which is the product of price
and number_of_reviews. Then compute the logarithm of the revenue using the same
method show in the previous question (“log_revenue”). And lastly visualize the
“log_revenue” distribution across different “neighbourhood_group” using a violin plot.
(10%)
df['revenue'] = df['price'] * df['number_of_reviews']
df['log_revenue'] = df['revenue'].apply(lambda x: [Link](x) if x > 0 else 0)
[Link](figsize=(10, 6))
[Link](x='neighbourhood_group', y='log_revenue', data=df)
[Link]('cs8366')
[Link]('Borough')
[Link]('Log Revenue')
plt.tight_layout()
[Link]()
6. Do Manhattan properties also generate the highest median revenue? (5%)
No, Manhattan does not generate the highest median revenue. Staten Island appears to have
the highest median revenue.