Welcome to New York City, one of the most-visited cities in the world. There are many Airbnb listings in New York City to meet the high demand for temporary lodging for travelers, which can be anywhere between a few nights to many months. In this project, we will take a closer look at the New York Airbnb market by combining data from multiple file types like .csv, .tsv, and .xlsx.
Recall that CSV, TSV, and Excel files are three common formats for storing data. Three files containing data on 2019 Airbnb listings are available to you:
data/airbnb_price.csv This is a CSV file containing data on Airbnb listing prices and locations.
listing_id: unique identifier of listingprice: nightly listing price in USDnbhood_full: name of borough and neighborhood where listing is located
data/airbnb_room_type.xlsx This is an Excel file containing data on Airbnb listing descriptions and room types.
listing_id: unique identifier of listingdescription: listing descriptionroom_type: Airbnb has three types of rooms: shared rooms, private rooms, and entire homes/apartments
data/airbnb_last_review.tsv This is a TSV file containing data on Airbnb host names and review dates.
listing_id: unique identifier of listinghost_name: name of listing hostlast_review: date when the listing was last reviewed
# Import necessary packages
import pandas as pd
import numpy as np
# Load the data files
price_df = pd.read_csv("data/airbnb_price.csv")
rooms_df = pd.read_excel("data/airbnb_room_type.xlsx")
reviews_df = pd.read_csv("data/airbnb_last_review.tsv", sep="\t")
# Clean the price column: remove $, commas, words like 'dollars', etc.
price_df['price'] = (
price_df['price']
.astype(str)
.str.replace(r'[^\d.]', '', regex=True) # Remove everything except digits and periods
.replace('', np.nan) # Handle empty strings
.astype(float)
)
# Convert the review dates to datetime objects
reviews_df['last_review'] = pd.to_datetime(reviews_df['last_review'], errors='coerce')
# Determine the earliest and most recent review dates
first_reviewed = reviews_df['last_review'].min()
last_reviewed = reviews_df['last_review'].max()
# Count the number of listings that are private rooms (case-insensitive)
nb_private_rooms = rooms_df[rooms_df['room_type'].str.lower() == 'private room'].shape[0]
# Compute the average listing price, rounded to two decimal places
avg_price = round(price_df['price'].mean(), 2)
# Combine the variables into one DataFrame called review_dates
review_dates = pd.DataFrame({
'first_reviewed': [first_reviewed],
'last_reviewed': [last_reviewed],
'nb_private_rooms': [nb_private_rooms],
'avg_price': [avg_price]
})
# Display the summary DataFrame
print(review_dates)