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
# loadiing the data
df1 = pd.read_csv("data/airbnb_price.csv")
df2 = pd.ExcelFile("data/airbnb_room_type.xlsx")
df3 = pd.read_csv("data/airbnb_last_review.tsv", sep = "\t")
df2 = df2.parse("airbnb_room_type")
print(df1.head())
print()
print(df2.head())
print()
print(df3.head())
#merging dataframes
first_merge = pd.merge(df1, df2, on = "listing_id", how = "inner")
df = pd.merge(first_merge, df3, on = "listing_id", how = "inner")
print()
print(df.head())
#working on the price column
df["price"] = df["price"].str.replace("dollars", "")
df["price"] = df["price"].str.strip()
df["price"] = df["price"].astype("int")
# working on the last_review column
df['last_review'] = pd.to_datetime(df["last_review"])
#confirming our transformation
print(df.info())
## the earliest reviews
first_review = df["last_review"].min()#.strftime(" %A, %d %B %Y ")
print(f"The earliest review was done on {first_review}")
# the most recent review
recent_review = df["last_review"].max()#.strftime(" %A, %d %B %Y ")
print(f"The most recent review was done on {recent_review}")
df["room_type"] = df["room_type"].str.upper()
df["room_type"].unique()
#number of private rooms in the listing
private_rooms = (df["room_type"] == "PRIVATE ROOM").sum()
print(f"The number of private rooms in the listing is {private_rooms}")
#outliers seem to affect the data cos some of the prices are tooo high , hence we use the median to caculate the average
avg_price = np.mean(df["price"])
print(round(avg_price, 2))
review_dict = {
"first_reviewed" : [first_review],
"last_reviewed" : [recent_review],
"nb_private_rooms" : [private_rooms],
"avg_price" : [avg_price]
}
# transforming the dictionary into a datfranne
review_dates = pd.DataFrame(review_dict)
print(review_dates)