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
# Reading the three files 
csv_df = pd.read_csv('data/airbnb_price.csv')
tsv_df = pd.read_csv('data/airbnb_last_review.tsv', sep='\t')
xlsx_df = pd.read_excel('data/airbnb_room_type.xlsx')
# Looking at the three datasets
print(csv_df.head())
print(tsv_df.head())
print(xlsx_df.head())
# Merging the three datasets on 2 steps
df1 = pd.merge(csv_df, tsv_df, on='listing_id')
df = pd.merge(df1, xlsx_df, on='listing_id')
df.head(5)
# Converting last_review to date format
df['last_review'] = pd.to_datetime(df['last_review'])
print(df['last_review'].head())
print(df.dtypes)
df.head()
# Finding the earliest and the most recent reviews
earliest_review = df['last_review'].min().date()
recent_review = df['last_review'].max().date()
print(f'The earliest review was at {earliest_review} and the most recent one was at {recent_review}')
# Finding how many listings are private
print(df['room_type'].head(10))

# Fix the capitalization error
df['room_type'] =  df['room_type'].str.capitalize()
print(df['room_type'].head(10))

Num_private = df[df['room_type']=='Private room'].shape[0]
print(f'The number of private rooms is {Num_private}')
# Finding the average price listing
print(df['price'].head(10))
# Converting the column to numeric type
df['price'] = df['price'].str.replace('dollars','').astype(int)
print(df['price'].head(10))
# The Mean
avg_price = df['price'].mean().round(2)
print(avg_price)
# Combining the new variables into one DataFrame 
review_dates = pd.DataFrame({'first_reviewed': [earliest_review], 'last_reviewed': [recent_review], 'nb_private_rooms': [Num_private], 'avg_price': [avg_price]})
review_dates.head()