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 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
# Import necessary packages
import pandas as pd
import numpy as np
# Load the Data using pd.read_csv() method
# For Excel File use pd.read_excel() method
airbnb_price = pd.read_csv('data/airbnb_price.csv')
airbnb_room_type = pd.read_excel('data/airbnb_room_type.xlsx')
airbnb_last_review = pd.read_csv('data/airbnb_last_review.tsv', sep='\t')
# Merge the three DataFrames using pd.merge() method with "on" parameter inside the method
listing = pd.merge(airbnb_price, airbnb_room_type, on='listing_id')
listing = pd.merge(listing, airbnb_last_review, on='listing_id')
# Convert reviews data to a date format using pd.to_datetime() function specifying the desired date on format parameter
# After converting reviews data to datetime format, use .min() & .max() functions to find the oldest and most recent reviews respectively
listing['last_review_date'] = pd.to_datetime(listing['last_review'], format='%B %d %Y')
first_reviewed = listing['last_review_date'].min()
last_reviewed = listing['last_review_date'].max()
# For Cleaning Data, use str.lower() method on room_type column
# To count the number of private rooms, filter the room_type column. Use .shape[0] for desired count
listing['room_type'] = listing['room_type'].str.lower()
private_room_count = listing[listing['room_type'] == 'private room'].shape[0]
# Convert price data to float values using .str.replace and chain .astype() to convert to float
# Find the mean of the price column
listing['price'] = listing['price'].str.replace(' dollars', '').astype(float)
avg_price = listing['price'].mean()
# Create a DataFrame containing four columns and round off avg_price to two decimal places
review_dates = pd.DataFrame({
'first_reviewed': [first_reviewed],
'last_reviewed': [last_reviewed],
'nb_private_rooms': [private_room_count],
'avg_price': [round(avg_price, 2)]
})
print(review_dates)