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

1. Loading the data

# Loading price data
price_df = pd.read_csv('data/airbnb_price.csv', index_col=0)

# Convert price to integer to enable numerical analysis
price_df['price'] = price_df['price'].str.strip(' dollars')
price_df['price'] = price_df['price'].astype('float')

###    ###    ###

# Loading room type data
room_obj = pd.ExcelFile('data/airbnb_room_type.xlsx')
room_df = room_obj.parse(0, header=0, index_col=0)

# Standardize room_type to lowercase for consistent grouping
room_df['room_type'] = room_df['room_type'].str.lower().str.strip()

###    ###    ###

# Loading review data
review_df = pd.read_csv('data/airbnb_last_review.tsv', delimiter='\t', index_col=0)

# Convert column to datetime objects to allow for chronological analysis.
review_df['last_review'] = pd.to_datetime(review_df['last_review'])

2. Merging the three DataFrames

# Unify dataframes to centralise analysis
df = price_df.merge(room_df, on='listing_id') \
             .merge(review_df, on='listing_id')

3. Determining the earliest and most recent review dates

# Determine most recent and oldest review in the dataframe
oldest_review = df['last_review'].min()
most_recent_review = df['last_review'].max()

4. Finding how many listings are private rooms

# Grouping room types to find how many there are of each type
room_groups = df.value_counts('room_type')
private_rooms = room_groups['private room']

5. Finding the average price of listings

# Averaging price with specified decimal places
avg_price = df['price'].mean().round(2)

6. Creating a DataFrame with the four solution values

anwser_dict = {'first_reviewed':oldest_review, 'last_reviewed':most_recent_review, 'nb_private_rooms':private_rooms, 'avg_price':avg_price}

review_dates = pd.DataFrame(anwser_dict, index=[0])

# Final anwser
print(review_dates)