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
# Begin coding here ...
# Use as many cells as you like
# Corrected file paths
df = pd.read_csv('./data/airbnb_price.csv')
df2 = pd.read_excel('./data/airbnb_room_type.xlsx')
df3 = pd.read_csv('./data/airbnb_last_review.tsv', sep='\t')
# Display the first few rows of the dataframe
df3.head()
df_merged = pd.merge(df, df2, on=['listing_id'])
df_merged
final_df = pd.merge(df_merged, df3, on=['listing_id'])
final_df
from datetime import datetime
import pandas as pd
# Ensure 'last_review' is in datetime format
final_df['last_review'] = pd.to_datetime(final_df['last_review'], format='%B %d %Y')
first_reviewed = final_df['last_review'].min()
last_reviewed = final_df['last_review'].max()
final_df['room_type'] = final_df['room_type'].str.lower()
private_rooms = final_df[final_df['room_type'] == 'private room']
nb_private_rooms = private_rooms.shape[0]
nb_private_rooms
# Ensure the 'price' column is of string type before using the .str accessor
final_df['price'] = final_df['price'].astype(str).str.replace('dollar', '').str.extract('(\d+)').astype('Int64')
avg_price = final_df['price'].mean().round(2)
avg_price
# creating the DataFrame from the variables
review_dates = pd.DataFrame({
'first_reviewed': [first_reviewed],
'last_reviewed' : [last_reviewed],
'nb_private_rooms': [nb_private_rooms],
'avg_price': [avg_price]
})
review_dates