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 notebook, 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
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
Our goals are to convert untidy data into appropriate formats to analyze, and answer key questions including:
- What is the average price, per night, of an Airbnb listing in NYC?
- How does the average price of an Airbnb listing, per month, compare to the private rental market?
- How many adverts are for private rooms?
- How do Airbnb listing prices compare across the five NYC boroughs?
#import data
import numpy as np
import pandas as pd
import datetime as dt#Load airbnb_price.csv as a dataframe called prices
prices = pd.read_csv("data/airbnb_price.csv")#Step 1: Load airbnb_room_type.xlsx as a dataframe called xls
xls = pd.ExcelFile("data/airbnb_room_type.xlsx")#Parse the first sheet from xls called room_types
room_types = xls.parse(0)
#Load "data/airbnb_last_review.tsv" as a dataframe called reviews
#pass '\t' to the sep argument of pd.read_csv()
reviews = pd.read_csv("data/airbnb_last_review.tsv", sep='\t') #Step 2: Clean the price column to calculate average prices.
#Update the 'price' column of the prices dataframe
#Replace the words 'pounds' and preceding whitespace from a column replacing with an empty string
prices["price"] = prices["price"].str.replace(" dollars", "")#Convert the price column data type to numeric by passing it to the pandas .to_numeric() method.
prices["price"] = pd.to_numeric(prices["price"])
#Step 3: Calculate the average price
# Subset prices for listings costing $0, free_listings
free_listings = prices["price"] == 0
# Update prices by removing all free listings from prices
prices = prices.loc[~free_listings]# Calculate the average price, round to two decimal places
avg_price = round(prices["price"].mean(), 2)# Step 4. Comparing costs to the private rental market
#convert the per night prices of the listings into monthly costs, to compare to the private market.
# Add a new column to the prices DataFrame, price_per_month
prices["price_per_month"] = prices["price"] * 365 / 12# Calculate average_price_per_month
average_price_per_month = round(prices["price_per_month"].mean(), 2)
difference = round((average_price_per_month - 3100),2)# Step 5. Cleaning the room_type column
# Convert the room_type column to lowercase
room_types["room_type"] = room_types["room_type"].str.lower()# Update the room_type column to category data type
room_types["room_type"] = room_types["room_type"].astype("category")