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# loading all three datasets
df_airbnb_price = pd.read_csv('data/airbnb_price.csv')
df_airbnb_room_type = pd.read_excel('data/airbnb_room_type.xlsx')
df_airbnb_last_review = pd.read_csv('data/airbnb_last_review.tsv', sep = '\t')# merging all three dfs
df_airbnb = df_airbnb_price.merge(df_airbnb_last_review, on = 'listing_id', how = 'outer')
df_airbnb_final = df_airbnb.merge(df_airbnb_room_type, on = 'listing_id', how = 'outer')# converting last_review column to datetime
df_airbnb_final['last_review'] = pd.to_datetime(df_airbnb_final['last_review'])
df_airbnb_final['last_review'] = df_airbnb_final['last_review'].dt.normalize()
# finding the earliest review date
earliest_date = df_airbnb_final['last_review'].min()
most_recent_date = df_airbnb_final['last_review'].max()# convert all text to lower case
df_airbnb_final['room_type'] = df_airbnb_final['room_type'].str.lower()
#getting the count for only 'private room'
private_room_count = (df_airbnb_final['room_type'] == 'private room').sum()
print(private_room_count)
print(df_airbnb_final.shape)df_airbnb_final.columns# Ensure the 'price' column is of string type before using .str methods
df_airbnb_final['price'] = df_airbnb_final['price'].astype(str).str.replace(' dollars', '').astype('float')
# avg listing price
avg_listing_price = df_airbnb_final['price'].mean()# Corrected code
import pandas as pd
new_df = {
'first_reviewed': earliest_date,
'last_reviewed': most_recent_date,
'nb_private_rooms': private_room_count,
'avg_price': round(avg_listing_price, 2)
}
# Use pd.DataFrame and wrap the dictionary in a list to create a single-row DataFrame
review_dates = pd.DataFrame([new_df])review_dates