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
# Import necessary packages
import pandas as pd
import numpy as np

# Begin coding here ...
# Use as many cells as you like

Loading the data

# Import airbnb price data
airbnb_price = pd.read_csv('data/airbnb_price.csv')
# Import airbnb room type data
airbnb_room = pd.read_excel('data/airbnb_room_type.xlsx')
# Import airbnb last review data
airbnb_review = pd.read_csv('data/airbnb_last_review.tsv',delimiter= '\t')

print(airbnb_price.head())
print(airbnb_review.head())
print(airbnb_room.head())

Determining the earliest and most recent review dates

# Converting the airbnb last_review to datetime type
airbnb_review['last_review'] = pd.to_datetime(airbnb_review['last_review'],format ='%B %d %Y')
earliest_review = airbnb_review['last_review'].min()
most_recent_review = airbnb_review['last_review'].max()
print(earliest_review,most_recent_review)

Finding how many listings are private rooms

# Check the room_type column categories
print(airbnb_room['room_type'].unique())
# Clean the room type column
airbnb_room['room_type'] = airbnb_room['room_type'].str.lower()
print(airbnb_room['room_type'].unique())
# Count the number of values off private rooms
room_type_counts  = airbnb_room.groupby('room_type')['listing_id'].count()
print(room_type_counts)
# Save the listings of private rooms
num_of_private_rooms = 11356

Finding the average price of listings

# Import re package
import re
# extract the numbers from the price column and transform it to float
def convert_to_float(text):
    match = re.search('\d+',text)
    return float(match.group())
airbnb_price['price'] = airbnb_price['price'].apply(convert_to_float)
avg_price = round(airbnb_price['price'].mean(),2)
print(avg_price)

Creating a DataFrame with the four solution values

data_dict = {'first_reviewed': earliest_review,'last_reviewed' : most_recent_review, 'nb_private_rooms' : num_of_private_rooms, 'avg_price' : avg_price}
review_dates = pd.DataFrame(data_dict,index=[0])
print(review_dates.head())