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 likeairbnb_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')
airbnb_last_review.head()# What are the dates of the earliest and most recent reviews? Store these values as two separate variables with your preferred names.
airbnb_last_review.info()
print('\n')
# Converting 'last_review' column to datetime data type
airbnb_last_review['last_review'] = pd.to_datetime(airbnb_last_review['last_review'], infer_datetime_format=True, errors = 'coerce')
airbnb_last_review.info()
# Checking if there are any null values is the last_review column
print("Number of null entries in the last_review column: ", airbnb_last_review['last_review'].isnull().sum())
airbnb_last_review.head()
earliest_review = airbnb_last_review['last_review'].min().date()
latest_review = airbnb_last_review['last_review'].max().date()
print("Earliest Review: ", earliest_review)
print("Latest Review: ", latest_review)
# Merging the three datasets
airbnb_data = airbnb_room_type.merge(airbnb_price, on = 'listing_id')
airbnb_data = airbnb_data.merge(airbnb_last_review, on = 'listing_id')
airbnb_data.head(20)# How many of the listings are private rooms? Save this into any variable.
airbnb_data['room_type'] = airbnb_data['room_type'].str.upper()
airbnb_data['room_type'].value_counts()
private_room_count = airbnb_data['room_type'].value_counts().get('PRIVATE ROOM', 0)
print("The number of private rooms in the airbnb are: ", private_room_count)# What is the average listing price? Round to the nearest two decimal places and save into a variable.
# Stripping the dollars from the price column and converting it into float
airbnb_data['price'] = airbnb_data['price'].astype('str').str.strip(" dollars").astype('float')
average_listing_price = round(airbnb_data['price'].mean(), 2)
print("The average airbnb listing price is: ", average_listing_price)
#airbnb_data.info()# Combine the new variables into one DataFrame called review_dates with four columns in the following order: first_reviewed, last_reviewed, nb_private_rooms, and avg_price. The DataFrame should only contain one row of values.
dict = {"first_reviewed": [earliest_review], "last_reviewed": [latest_review] , "nb_private_rooms": [private_room_count], "avg_price": [average_listing_price]}
review_dates = pd.DataFrame(dict)
print(review_dates)