Skip to content

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 listing
  • price: nightly listing price in USD
  • nbhood_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 listing
  • description: listing description
  • room_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 listing
  • host_name: name of listing host
  • last_review: date when the listing was last reviewed
# We've loaded your first package for you! You can add as many cells as you need.
import numpy as np

# Begin coding here ...
import pandas as pd

# LOADING THE DATA ------------------------------------------------------->
# Load airbnb_price.csv
prices = pd.read_csv("data/airbnb_price.csv")

# Load airbnb_room_type.xlsx
xls = pd.ExcelFile("data/airbnb_room_type.xlsx")

# Parse the 'airbnb_room_type' sheet from xls
room_types = xls.parse('airbnb_room_type')

# Load airbnb_last_review.tsv
reviews = pd.read_csv("data/airbnb_last_review.tsv", sep="\t")

# MERGING THE THREE DATAFRAMES ------------------------------------------->
# Merge prices and room_types to create rooms_and_prices
rooms_and_prices = pd.merge(prices, room_types, how='outer', on='listing_id')

# Merge rooms_and_prices with the reviews DataFrame to create airbnb_merged
airbnb_merged = pd.merge(rooms_and_prices, reviews, how='outer', on='listing_id')

#DETERMINING THE EARLIEST AND MOST RECENT REVIEW DATES ------------------->
#Convert the review dates to date format
airbnb_merged['last_review']=pd.to_datetime(airbnb_merged['last_review']).dt.date

#Determine the earliest review date
first_reviewed = airbnb_merged['last_review'].min()

#Determine the recent review date
last_reviewed = airbnb_merged['last_review'].max()

#FINDING HOW MANY LISTINGS ARE PRIVATE ROOMS----------------------------->
# Convert the room_type column to lowercase
airbnb_merged['room_type']= airbnb_merged['room_type'].str.lower()

# Update the room_type column to category data type
airbnb_merged['room_type']= airbnb_merged['room_type'].astype('category')

# Filter the data to include only private rooms
nb_private_rooms = airbnb_merged[airbnb_merged['room_type'] == 'private room'].shape[0]

#FINDING THE AVERAGE PRICE OF LISTINGS ----------------------------------->
#Convert the price data to float values
airbnb_merged["price"] = airbnb_merged["price"].str.replace(" dollars", "")

# Convert prices column to numeric datatype
airbnb_merged["price"] = pd.to_numeric(airbnb_merged["price"])

#Clean the data because there are some outliers where the max is 7500 and the minimum is 0 which means free
# Subset prices for listings costing $0 named "free_listings"
#free_listings = airbnb_merged["price"] == 0

# Update prices by removing all free listings from prices
# Similar to SQL's concept of "NOT IN"
#airbnb_merged["price"] = airbnb_merged["price"].loc[~free_listings]

# Calculate the average price and round to nearest 2 decimal places, avg_price
avg_price = airbnb_merged["price"].mean()

#CREATING A DATAFRAME WITH THE FOUR SOLUTION VALUES
# Create a DataFrame with the desired column names and corresponding values
review_dates = pd.DataFrame(
    {'first_reviewed': [first_reviewed],
    'last_reviewed': [last_reviewed],
    'nb_private_rooms': [nb_private_rooms],
    'avg_price': [round(avg_price,2)]}
)

#Print the results
print(review_dates)