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 pandas as pd
# Load data from CSV, Excel, and TSV files
airbnb_price_df = pd.read_csv("data/airbnb_price.csv")
airbnb_room_type_df = pd.read_excel("data/airbnb_room_type.xlsx")
airbnb_last_review_df = pd.read_csv("data/airbnb_last_review.tsv", delimiter="\t")
# Merge dataframes into a single DataFrame
df = airbnb_price_df.merge(airbnb_room_type_df, on="listing_id").merge(
airbnb_last_review_df, on="listing_id"
)
# Clean and preprocess the data
df["last_review"] = pd.to_datetime(df["last_review"])
df["price"] = df["price"].str.replace(" dollars", "").astype(int)
df["room_type"] = df["room_type"].str.lower()
# Task 1: Find the earliest and most recent review dates
earliest_review_date = df["last_review"].min()
most_recent_review_date = df["last_review"].max()
# Task 2: Count the number of private rooms
num_private_rooms = df["room_type"].value_counts().get('private room', 0)
# Task 3: Calculate the average listing price
average_listing_price = df["price"].mean().round(2)
# Task 4: Combine results into a new DataFrame
review_data = pd.DataFrame(
{
"first_reviewed": [earliest_review_date],
"last_reviewed": [most_recent_review_date],
"nb_private_rooms": [num_private_rooms],
"avg_price": [average_listing_price]
}
)
# Print the review data
print(review_data)