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
# load the csv file for Airbnb listing price
airbnb_price_df = pd.read_csv('data/airbnb_price.csv')
# Load the Excel file for Airbnb listing decription
airbnb_room_type_df = pd.read_excel('data/airbnb_room_type.xlsx')
# Load the TSV file for Airbnb host names and review dates
airbnb_last_review_df = pd.read_csv('data/airbnb_last_review.tsv', sep='\t')
# get info for all data
airbnb_price_df.info()
airbnb_room_type_df.info()
airbnb_last_review_df.info()
#merging airbnb_price_df and airbnb_room_type_df
mdf = pd.merge(airbnb_price_df, airbnb_room_type_df, on='listing_id', how='inner')
#merging mdf and air_bnb_last_review_df
merged_df = pd.merge(mdf, airbnb_last_review_df, on='listing_id', how='inner')
#check merged data
merged_df.head()
# check for data information
merged_df.info()
# Convert 'room_type' to lowercase and remove extra spaces
merged_df['room_type'] = merged_df['room_type'].str.strip().str.lower()
# Convert 'last_review' to datetime format
merged_df['last_review'] = pd.to_datetime(merged_df['last_review'], format='%B %d %Y')
# Split 'price' into 'price_amount' and 'currency'
merged_df[['price_amount', 'currency']] = merged_df['price'].str.split(' ', expand=True, n=1)
# Convert 'price_amount' to float
merged_df['price_amount'] = merged_df['price_amount'].astype(float)
# Get the date of the earliest review
first_reviewed = merged_df['last_review'].min()
print("First reviewed date:", first_reviewed)
# Get the date of the most recent review
last_reviewed = merged_df['last_review'].max()
print("Last reviewed date:", last_reviewed)
#number of private rooms
nb_private_rooms = merged_df['room_type'].value_counts().get('private room', 0)
print("Number of private rooms:", nb_private_rooms)
#average price
average_price = merged_df['price_amount'].mean()
avg_price = round(average_price, 2)
print("Average price (rounded to two decimal places):", avg_price)
#Combining the new variables into one DataFrame called review_dates
review_dates = pd.DataFrame({
'first_reviewed': [first_reviewed],
'last_reviewed': [last_reviewed],
'nb_private_rooms': [nb_private_rooms],
'avg_price': [avg_price]
})
# print DataFrame
print(review_dates)