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
airbnb_price = pd.read_csv('data/airbnb_price.csv')
airbnb_room_type = pd.read_excel('data/airbnb_room_type.xlsx')
airbnb_last_review = pd.read_csv('data/airbnb_last_review.tsv', sep='\t')
airbnb_last_review
# ----------------- Cleaning airbnb_price DataFrame -----------------
airbnb_price['price'] = airbnb_price['price'].astype(str)
airbnb_price['price'] = (
airbnb_price['price']
.str.replace('dollars', '', regex=False)
.str.strip()
)
airbnb_price['price'] = airbnb_price['price'].astype(int)
# ----------------- Cleaning airbnb_room_type DataFrame -----------------
airbnb_room_type['room_type'] = airbnb_room_type['room_type'].str.lower()
# ----------------- Cleaning airbnb_last_review DataFrame -----------------
airbnb_last_review['last_review'] = pd.to_datetime(airbnb_last_review['last_review'], errors='coerce')
# -------- What are the dates of the earliest and most recent reviews? --------
oldest_date = airbnb_last_review['last_review'].min()
latest_date = airbnb_last_review['last_review'].max()
print(oldest_date)
print(latest_date)
# ---------- How many of the listings are private rooms? Save this into any variable? ----------
private_rooms = (airbnb_room_type
.groupby('room_type')
.count()
.loc['private room', 'listing_id'])
print(private_rooms)
# ---------- What is the average listing price? ----------
average_price = airbnb_price['price'].mean().round(2)
print(average_price)
# ---------- Combine the new variables into one DataFrame ----------
dict = {
'first_reviewed':[oldest_date],
'last_reviewed':[latest_date],
'nb_private_rooms':[private_rooms],
'avg_price':[average_price]
}
review_dates = pd.DataFrame(dict)
print(review_dates)
# ----------------- Merging Three DataFrames -----------------
airbnb = (
airbnb_price
.merge(airbnb_room_type, on='listing_id', how='inner')
.merge(airbnb_last_review, on='listing_id', how='inner')
)