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
# Begin coding here ...
# Use as many cells as you like
# Overview the different datasets and explore their information
bnb_price = pd.read_csv("data/airbnb_price.csv")
bnb_room = pd.read_excel("data/airbnb_room_type.xlsx")
bnb_review = pd.read_csv("data/airbnb_last_review.tsv", sep = "\t", parse_dates=["last_review"])
print("AIRBNB PRICE HEAD\n", bnb_price.head())
print("\nAIRBNB ROOM TYPE\n", bnb_room.head())
print("\nAIRBNB LAST REVIEW\n" , bnb_review.head())# We can merge all three data frames into one to get the full overview of all data
# We can do this by doing inner join on them in the `listing_id` column
bnb_room_price = pd.merge(bnb_price, bnb_room, on='listing_id', how='inner')
bnb_all = pd.merge(bnb_room_price, bnb_review, on='listing_id', how='inner')
print(bnb_all.head())
# What are the dates of the earliest and more recent reviews?
# min and max of dates
# Store are two variables
earliest = bnb_all["last_review"].min()
latest = bnb_all["last_review"].max()
print(earliest, latest)
# How many listings are private rooms?
# Can filter by private rooms and use count to get the total number
print(bnb_all["room_type"].unique())
# From this we see that there are inconsistencies with capitalization. We fix this by having them follow a standard rule, in this case they will all be converted to lowercase since there are no spelling issues
bnb_all["room_type"] = bnb_all["room_type"].str.lower()
print(bnb_all["room_type"].head())
# Get the total no. of private rooms
bnb_priv = bnb_all[bnb_all["room_type"] == "private room"]
total_priv = bnb_priv["room_type"].count()
print(total_priv)# What is the average listing price? Round to nearest 2 decimal places
# Make sure that we are working on a correct data type
# Strip the word `dollars` since we want this column to be of numeric datatype to get the mean
bnb_all["price"] = bnb_all["price"].str.replace(" dollars", "")
# Convert the column into numeric data type
bnb_all["price"] = bnb_all["price"].astype('float')
print(bnb_all["price"].head())
average_price = round(bnb_all["price"].mean(), 2)
print(average_price)# Combine all variables into one DataFrame called `review_dates`
# Four different columns: `first_reviewed`, `last_reviewed`, `nb_private_rooms`, `avg_price`
data = [{'first_reviewed':earliest, 'last_reviewed':latest, 'nb_private_rooms':total_priv, 'avg_price':average_price}]
review_dates = pd.DataFrame(data)
print(review_dates.head())